如何使用Option标签中的Action Link向控制器发送参数



我使用选择标签,我有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没有额外的路由参数(subSectionIDsubsectionName)。因此,Update动作方法将subSectionIDsubsectionName参数接收为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.Actionhelper而不是Html.ActionLinkUrl.Action帮助器通过使用指定的操作名称和路由值来生成一个完全限定的URL。

但是Html.ActionLink为指定的链接文本、动作返回一个锚元素,这可能会在传递到服务器端时导致额外的问题。

最新更新