我使用选择标签,我有foreach
,将值添加到列表中,但我必须调用更新控制器并使用ActionLink
传递2个参数。我试过这样做,但行不通。我想知道我做错了什么?
<form action="Update " method="get" class="select-menu">
<select id="sectionId"
name="sectionId"
class="selectpicker"
title="Section"
data-width="100%"
data-live-search="true"
onchange="this.form.submit()">
@foreach (var item in Model)
{
<option value="@item.Text" data-url="@Html.ActionLink(item.Text, "UpdateBoard", new { subSectionID = item.Value, subsectionName = item.Text })"></option>
}
</select>
</form>
查询应该是这样的http://localhost:60082/Update?subSectionID=27&subsectionName=Something
谢谢!
在form
中使用select
标记将无法正常工作,因为form
没有额外的路由参数(subSectionID
和subsectionName
)。因此,Update
动作方法将subSectionID
和subsectionName
参数接收为null
。
因此,要根据选择动态设置这些参数,请尝试以下操作:
<script type="text/javascript">
$('#sectionId').change(function () {
var url = $(this).val();
if (url != null && url != '') {
window.location.href = url;
}
})
</script>
<select id="sectionId"
name="sectionId"
class="selectpicker"
title="Section"
data-width="100%"
data-live-search="true">
@foreach (var item in Model)
{
<option value="@Url.Action("SetLanguage", new { subSectionID = item.Value, subsectionName = item.Text })">@item.Text</option>
}
</select>
在foreach
中使用Url.Action
helper而不是Html.ActionLink
。Url.Action
帮助器通过使用指定的操作名称和路由值来生成一个完全限定的URL。
但是Html.ActionLink
为指定的链接文本、动作返回一个锚元素,这可能会在传递到服务器端时导致额外的问题。