在 ASP.NET MVC 视图中自动发布单选按钮

发布于 2024-08-06 04:29:05 字数 467 浏览 1 评论 0原文

我正在尝试在视图顶部添加几个单选按钮(使用 ASP.NET MVC)。

我希望两个单选按钮(选项 1 和选项 2)使页面在更改时刷新/自动发布。

这两个单选按钮位于单独的 .asxc (Menu.ascx) 中,并通过 Html.RenderPartial 添加到 List.aspx 页面中。这两个选项依次确定是否将 Html.RenderPartial("ListOption1Elements") 或 Html.RenderPartial("ListOption2Elements") 添加到页面。

我尝试添加 Html.RadioButton 并使用,但尚未正常工作。其中只有最后一个具有我需要的 AutoPostBack 属性,但它抱怨不在标签内。

所以,总结一下 - 如何添加两个硬编码的单选按钮,这将导致回发,以便我在控制器中可以确定要渲染哪个部分视图?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆凄美了谁 2024-08-13 04:29:05

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.

脱离于你 2024-08-13 04:29:05

您应该尝试通过 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文