在 ASP.NET MVC 视图中自动发布单选按钮
我正在尝试在视图顶部添加几个单选按钮(使用 ASP.NET MVC)。
我希望两个单选按钮(选项 1 和选项 2)使页面在更改时刷新/自动发布。
这两个单选按钮位于单独的 .asxc (Menu.ascx) 中,并通过 Html.RenderPartial 添加到 List.aspx 页面中。这两个选项依次确定是否将 Html.RenderPartial("ListOption1Elements") 或 Html.RenderPartial("ListOption2Elements") 添加到页面。
我尝试添加 Html.RadioButton 并使用
所以,总结一下 - 如何添加两个硬编码的单选按钮,这将导致回发,以便我在控制器中可以确定要渲染哪个部分视图?
I'm trying to add a couple of radiobuttons at the top of a View (using ASP.NET MVC).
I want the two radiobuttons (option 1 and option 2) to cause the page to be refreshed/autoposted when changed.
The two radiobuttons live inside a separate .asxc (Menu.ascx), and added with Html.RenderPartial in a List.aspx page. The two options in turn determine if Html.RenderPartial("ListOption1Elements") or Html.RenderPartial("ListOption2Elements") is added to the page.
I've tried adding both Html.RadioButton and using <asp:RadioButtonList>, but haven't got it working properly. Only the last one of those has the AutoPostBack property I need, but it complains about not being inside a tag.
So, to sum up - how can I add two hardcoded radiobuttons that will cause a postback so that I in my Controller can determine which partial view to render?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ASP.NET MVC 以及服务器控件一般不支持回发概念。
您需要使用 JavaScript 手动完成此操作。将函数附加到单选按钮的“onclick”事件,并对控制器操作进行异步调用,该操作将简单地向生成相关视图的同一操作发出服务器端重定向。
Postback concept is not supported in ASP.NET MVC as well as server controls in general.
You need to do it manually with JavaScript. Attach a function to the "onclick" event of a radiobutton and make an asynchronous call to a controller action that will simply issue a server side redirect to the same action that generated the view in question.
您应该尝试通过 javascript 来完成此操作:
MVC 中的 RadioButtonFor 回发
或者以 Ajax 方式执行此操作:
MVC - 单击单选按钮时回发
You should try this to do it by javascript:
Postback on RadioButtonFor in MVC
Or this to do it in Ajax way:
MVC - Postback on clicking a radio button