我有一个移动表单,其中包含一个位置下拉列表。当我从DDL中选择一个位置时,我希望表单提交给一个动作。
我的剃刀:
@Html.DropDownListFor(a => a.SelectedLocationID
, Model.AllLocation
, "Select your location")
我的Jquery:
$("#SelectedLocationID").on("change", function () {
//var $id = $(this).val();
$("form").submit();
//$("form").submit({ "id": $id }, test);
})
问题是该操作需要变量“id”,但所选的位置变量是SelectedLocationID。如何动态更改变量名称?
答案 0 :(得分:1)
在这种情况下,您不能使用DropDownListFor
,因为它是强类型的,并且名称是根据您作为参数传递的lambda表达式生成的。
您可以改为DropDownListFor
改为DropDownList
,并传递名称:
@Html.DropDownList("id", new SelectList(Model.AllLocation), "Select your location");
您将在行动方法中获得“id”。