MVC中的DropDown列表onchange事件和AJAX
我的 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"] 的值,以便我可以将其发送到所需的操作。 谁能建议我该怎么做?
谢谢!
I have a code block in my MVC view as follows:
<%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()" })%>
<%} %>
I want to set the value of ViewData["selected"] so that i can send it to the desired action.
Can anyone please suggest how can i do this?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ViewData 不是将数据传回服务器端的地方。表单标签内的 html 输入控件的值可以在操作方法中方便地获得。您可以从各种类型的操作方法参数(模型、表单集合等)获取这些值。
这是 免费的 ASP.NET MVC 电子书教程。是一个很好的asp.net mvc资源。
ViewData is not the place to pass data back to the server side. Values of html input controls within form tag are conveniently available in action method. You can get these values either from various types of action method arguments (model, formcollection etc).
Here is a link to free asp.net mvc ebook tutorial. Is a good resource for asp.net mvc.
在这篇文章中找到了解决方案 这只是一个小变化 是
的,没错 - 只是更改正在替换:
onchange =“this.form.submit();”
与:
onchange =“$(this.form).submit();”
Found solution at this post it is just small chnge
Yes, that’s right – only change is replacing:
onchange = “this.form.submit();”
with:
onchange = “$(this.form).submit();”
为什么不在下拉列表中使用 jQuery onChange 事件而不是使用表单呢?
Instead of using a form, why not use a jQuery onChange event on your drop down?