我使用的是一个选择标记,我有一个foreach,它将值添加到列表中,但是我必须调用一个Update,并使用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
谢谢!
发布于 2022-06-04 14:55:30
以上述方式在select中使用form标记将不能正常工作,因为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助手而不是Html.ActionLink。Url.Action助手使用指定的操作名称和路由值为操作方法生成完全限定的URL。
但是Html.ActionLink返回指定链接文本的锚点元素,操作,这可能会在传递到服务器端时引起额外的问题。
https://stackoverflow.com/questions/72492454
复制相似问题