我的MVC视图中的代码为黑色,如下所示:
<%using (Ajax.BeginForm("MyAction", new { action = "MyAction", controller = "Home", id = ViewData["selected"].ToString() }, new AjaxOptions { UpdateTargetId = "Div1" })) { %> <%=Html.DropDownList("ddl", ViewData["MyList"] as SelectList, new { onchange = "this.form.submit()" })%> <%} %>
我想设置ViewData [“ selected”]的值,以便可以将其发送到所需的操作。有人可以建议我该怎么做吗?
谢谢!
为什么不使用表单,而不在下拉菜单中使用jQuery onChange事件?
$(document).ready(function() { $("#ddl").change(function() { var strSelected = ""; $("#ddl option:selected").each(function() { strSelected += $(this)[0].value; }); var url = "/Home/MyAction/" + strSelected; $.post(url, function(data) { // do something if necessary }); }); });