I have buttonOnclick,which takes 2 parameters
我有buttonOnclick,它有2个参数
$('.Conteiner').on("click", ".Button", function () {
window.location.href = '@Url.Action("Form", "State", new {numberNights, datepicker})';
});
I need to pass parameters from @Url.Action to controller
我需要将参数从@ Url.Action传递给控制器
My controller:
public ActionResult Form(int numberNights, string datepicker)
{
@ViewBag.NumberNights = numberNights;
@ViewBag.DatePicker = datepicker;
return View();
}
Parameter numberNights is pass Ok, but datepicker always null. Why?
参数numberNights传递Ok,但是datepicker总是为null。为什么?
1 个解决方案
#1
First, you need to actually name the members of the anonymous object, i.e.:
首先,您需要实际命名匿名对象的成员,即:
new { numberOfNights = numberOfNights, datepicker = datepicker }
numberOfNights
, is probably coming through by sheer accident, since it's the first parameter of the action.
numberOfNights,可能是纯粹的意外,因为它是行动的第一个参数。
Second, make sure that you're not mixing client-side and server-side code. Namely, in order for a value to be passed for datepicker, it must be a C# variable defined in your Razor code, not a JavaScript variable. The value, also, must exist before your JavaScript has a chance to run. If you need to update it dynamically upon the user changing the datepicker value, you'll need to forgo passing it in Url.Action
.
其次,确保您没有混合客户端和服务器端代码。也就是说,为了为datepicker传递一个值,它必须是Razor代码中定义的C#变量,而不是JavaScript变量。在JavaScript有机会运行之前,该值也必须存在。如果您需要在用户更改datepicker值时动态更新它,则需要放弃在Url.Action中传递它。
#1
First, you need to actually name the members of the anonymous object, i.e.:
首先,您需要实际命名匿名对象的成员,即:
new { numberOfNights = numberOfNights, datepicker = datepicker }
numberOfNights
, is probably coming through by sheer accident, since it's the first parameter of the action.
numberOfNights,可能是纯粹的意外,因为它是行动的第一个参数。
Second, make sure that you're not mixing client-side and server-side code. Namely, in order for a value to be passed for datepicker, it must be a C# variable defined in your Razor code, not a JavaScript variable. The value, also, must exist before your JavaScript has a chance to run. If you need to update it dynamically upon the user changing the datepicker value, you'll need to forgo passing it in Url.Action
.
其次,确保您没有混合客户端和服务器端代码。也就是说,为了为datepicker传递一个值,它必须是Razor代码中定义的C#变量,而不是JavaScript变量。在JavaScript有机会运行之前,该值也必须存在。如果您需要在用户更改datepicker值时动态更新它,则需要放弃在Url.Action中传递它。