Razor中的Dropdown绑定onchange方法

Dropdown bind onchange method in Razor

本文关键字:onchange 方法 绑定 Dropdown 中的 Razor      更新时间:2023-09-26

我有以下下拉列表。如何将OnChange方法绑定到它?。

我使用的是代码优先的MVC方法。此外,当将change方法绑定到索引值时,如何获取该索引值?

 <div class="editor-label">
     @Html.LabelFor(model => model.Id, "Empl")
 </div>
 <div class="editor-field">
     @Html.DropDownList("Id", String.Empty)
     @Html.ValidationMessageFor(model => model.Id)
 </div>

您可以使用JQuery绑定更改,然后获取索引。

像这样的东西应该会解决它。

@Html.DropDownList("Id", Enumerable.Empty<SelectListItem>(), new { onchange = "changeFunction()" })

这是假设你想要一个空白列表。