如何将3个参数传递给控制器

时间:2021-05-28 18:04:31

This my controller,which takes date:

这是我的控制器,它取日期:

public ActionResult ViewLevel(string datepicker)
        {
            @ViewBag.DatePicker = datepicker;
            return View();
        }

I have View:

我有看法:

@model Car

@{
    var datepicker = Convert.ToString(ViewBag.DatePicker);
}
<div class = "Conteiner"
@foreach (Car car in Model)
        {
<p data-carnumber="@car.Number"> @car.Number<p>
<button class="buyCar">Buy</button>
}
</div>

<script type="text/javascript">
    $('.Conteiner').on("click", ".buyCar", function () {
        var number = $(this).data("carnumber");
        window.location.href = '@Html.Raw(Url.Action("Form", "Buy", new {datepicker,number}))';
    });
</script>

datepicker I need to pass too. Why not see number in new {datepicker,number} .number is red

datepicker我也需要通过。为什么看不到新{datepicker,number}中的数字.number为红色

3 个解决方案

#1


This is one reason I hate to see Razor injected into Javascript. It confuses the whole client/server separation (and it does not allow the scripts to be in separate JS files where debugging JS will actually work in Visual Studio!).

这是我讨厌看到Razor注入Javascript的一个原因。它混淆了整个客户端/服务器的分离(并且它不允许脚本位于单独的JS文件中,调试JS实际上可以在Visual Studio中工作!)。

You need to construct the parameter part of the URL at run-time on the client side, using the selected values:

您需要使用选定的值在客户端运行时构造URL的参数部分:

e.g.

 var staticUrl = "@Html.Raw(Url.Action("Form", "Buy", new { datepicker=datepicker}))"
 $('.Conteiner').on("click", ".buyCar", function () {
      var number = $(this).prev().data("carnumber");
    window.location.href = staticUrl + "&number=" + number;
});

Here I inject only the main part of the action URL and leave the parameters to be added at click time.

这里我只注入操作URL的主要部分,并保留在点击时添加的参数。

Normally I inject only the site root URL into a global var with something like this in the layout file:

通常我只将站​​点根URL注入到布局文件中具有类似内容的全局var中:

<script>
    window.siteRoot = @Url.Content("~/");
</script>

and use that URL in my JS/jQuery everywhere the site root is needed.

并在我的JS / jQuery中使用该URL,只需要站点根目录。

 $('.Conteiner').on("click", ".buyCar", function () {
      var number = $(this).prev().data("carnumber");
    window.location.href = siteRoot + "buy/form?datapicker=" + datepicker + "&number=" + number;
});

#2


It looks as though your problem is that you are trying to use a javascript variable in a razot Html helper parameter.

看起来你的问题是你试图在razot Html帮助器参数中使用javascript变量。

You cannot do this because razor syntax is executed on the server and javascript is executed client side.

你不能这样做,因为剃刀语法在服务器上执行,javascript在客户端执行。

Please see this answer

请看这个答案

#3


First, you have a syntax error:

首先,您有语法错误:

Correct would be new {parametername1=value1, parametername2=value2}.

正确将是新的{parametername1 = value1,parametername2 = value2}。

Seconds (as comments pointed out) you can't use a javascript variable (client/browser side) in an @Html Method (serverside). One trick I used to solve this problem is to use placeholders in the @Html.Action like new {date="###THEDATE###", number="###THENUMBER###"} and with the corresponding string that @Html returns (and which is sent to the browser and comes to rest in a javascript variable) on client side the placeholder are replaced with javascript using the string replace function.

秒(如评论所指出的)你不能在@Html方法(服务器端)中使用javascript变量(客户端/浏览器端)。我用来解决这个问题的一个技巧是在@Html.Action中使用占位符,如new {date =“### THEDATE ###”,number =“### THENUMBER ###”}以及相应的字符串@Html返回(并将其发送到浏览器并在javascript变量中休息)在客户端,占位符将使用字符串替换函数替换为javascript。

#1


This is one reason I hate to see Razor injected into Javascript. It confuses the whole client/server separation (and it does not allow the scripts to be in separate JS files where debugging JS will actually work in Visual Studio!).

这是我讨厌看到Razor注入Javascript的一个原因。它混淆了整个客户端/服务器的分离(并且它不允许脚本位于单独的JS文件中,调试JS实际上可以在Visual Studio中工作!)。

You need to construct the parameter part of the URL at run-time on the client side, using the selected values:

您需要使用选定的值在客户端运行时构造URL的参数部分:

e.g.

 var staticUrl = "@Html.Raw(Url.Action("Form", "Buy", new { datepicker=datepicker}))"
 $('.Conteiner').on("click", ".buyCar", function () {
      var number = $(this).prev().data("carnumber");
    window.location.href = staticUrl + "&number=" + number;
});

Here I inject only the main part of the action URL and leave the parameters to be added at click time.

这里我只注入操作URL的主要部分,并保留在点击时添加的参数。

Normally I inject only the site root URL into a global var with something like this in the layout file:

通常我只将站​​点根URL注入到布局文件中具有类似内容的全局var中:

<script>
    window.siteRoot = @Url.Content("~/");
</script>

and use that URL in my JS/jQuery everywhere the site root is needed.

并在我的JS / jQuery中使用该URL,只需要站点根目录。

 $('.Conteiner').on("click", ".buyCar", function () {
      var number = $(this).prev().data("carnumber");
    window.location.href = siteRoot + "buy/form?datapicker=" + datepicker + "&number=" + number;
});

#2


It looks as though your problem is that you are trying to use a javascript variable in a razot Html helper parameter.

看起来你的问题是你试图在razot Html帮助器参数中使用javascript变量。

You cannot do this because razor syntax is executed on the server and javascript is executed client side.

你不能这样做,因为剃刀语法在服务器上执行,javascript在客户端执行。

Please see this answer

请看这个答案

#3


First, you have a syntax error:

首先,您有语法错误:

Correct would be new {parametername1=value1, parametername2=value2}.

正确将是新的{parametername1 = value1,parametername2 = value2}。

Seconds (as comments pointed out) you can't use a javascript variable (client/browser side) in an @Html Method (serverside). One trick I used to solve this problem is to use placeholders in the @Html.Action like new {date="###THEDATE###", number="###THENUMBER###"} and with the corresponding string that @Html returns (and which is sent to the browser and comes to rest in a javascript variable) on client side the placeholder are replaced with javascript using the string replace function.

秒(如评论所指出的)你不能在@Html方法(服务器端)中使用javascript变量(客户端/浏览器端)。我用来解决这个问题的一个技巧是在@Html.Action中使用占位符,如new {date =“### THEDATE ###”,number =“### THENUMBER ###”}以及相应的字符串@Html返回(并将其发送到浏览器并在javascript变量中休息)在客户端,占位符将使用字符串替换函数替换为javascript。