Html.BeginForm 路由值包括表单单选按钮结果
在过去的几天里,我问了几个问题,可能没有得到回答,因为它可能太广泛了。经过一番研究,我可能已经缩小了范围。
我想知道是否有一种方法可以使用 Begin 表单的 Html.BeginForm(action,controller,routeValues) 重载来传递 RadioButton 结果(即用户单击了两个选项之一)。我需要将该值传递到查询字符串中,以便我的操作可以处理它。我的操作需要两个参数,到目前为止我只能弄清楚如何将这些值之一放入操作中。这是一些示例代码...
<%Html.BeginForm("TemplateInfo","PatientACO",new { PopulationID = ViewData["POPULATIONID"], ActiveAll = "1"});
%><input type="submit" value="Refresh" id="test" /><%
%></div>
<% Html.EndForm(); %>
注意,我正在神奇地设置我的 ActiveAll 属性 auto 。我需要它动态设置。 IE,需要用户设置。因为该特定操作需要两个参数。需要找到一种方法将动态价值融入到该行动中吗?我希望 RouteValue 字典或一般的 routValues 能够帮助我找到一种方法来完成这项工作。
如果您知道如何做到这一点,我将非常感激。
更新
我还想提一下,我不想更改控制器以接受 FormCollections,因为此操作是由其他方法和视图调用的。有什么想法吗?
更新2
好的,通过更多的研究,我想我想弄清楚的是,如何获取视图中单选按钮的表单值,以便然后我可以将它添加到 RouteValueDictionary 或其他东西中...有什么想法吗?
I have asked a couple of questions over the last couple of days, that probably did not get answered because it might have been to broad. After some research, I may have narrowed it down a little bit.
I am wondering if there is a way that I can use the Html.BeginForm(action,controller,routeValues) overload of Begin form to pass in RadioButton results (ie, the user has clicked one of the two choices). I need that value passed into the query string so that my action can handle it. My action takes two parameters, and so far I can only figure out how to get one of those values into the action. Here is some example code...
<%Html.BeginForm("TemplateInfo","PatientACO",new { PopulationID = ViewData["POPULATIONID"], ActiveAll = "1"});
%><input type="submit" value="Refresh" id="test" /><%
%></div>
<% Html.EndForm(); %>
Note, I am setting my ActiveAll attribute, auto Magically. I need it set dynamically. IE, it needs to be set by the user. Because that particular action takes two parameters. Need to figure out a way to get that dynamic value into that action? I am hoping that routeValue dictionaries, or routValues in general will help me figure out a way to get this done.
If you have any idea's how to do this I would really appreciate this.
UPDATE
I also wanted to mention that I don't want to change the controller to accept FormCollections, because this action is called by other methods and views. Any ideas?
UPDATE 2
Ok, with a little more research, I guess what I am trying to figure out is, how can I get the Form value of the Radio Button in the view so that I can then add it to a RouteValueDictionary or something... Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要将选定的值传递到查询字符串中。
只要单选按钮的名称与控制器操作中的变量相同,MVC 就会将表单值绑定到操作中的变量。
在您的示例中,如果您的表单有一个名为
ActionAll
的单选按钮列表,以及两个值为true
和false
的单选按钮,则如果您的控制器操作有一个名为ActionAll
的布尔变量,则单选按钮列表选择的结果将绑定到控制器操作的 ActionAll 变量。更新
根据您提供的信息,如果您的操作方法如下所示:
那么您可以通过以下方式从视图中的按钮列表传递 ActionAll 变量:
如果您在操作中设置断点,您将看到 ActionAll 已根据列表中选定的单选按钮进行设置。如果 ActionAll 不是布尔值,请相应地调整列表,以传递正确类型的值以绑定到 ActionAll。
You do not need to pass the selected values into a query string.
As long as the radio buttons have names equal to the variables you have in your controller action, MVC will bind the form values to the variables in your action.
In your example, if your form has a radio button list with the name
ActionAll
, and two radio buttons with values oftrue
andfalse
, then if your controller action has a boolean variable namedActionAll
, the results of your radio button list selection will be bound to your controller action's ActionAll variable.UPDATE
Based upon the information you provided, if your action method looks like this:
Then you can pass the ActionAll variable from a button list in your view with the following:
If you set a breakpoint in your action, you will see that ActionAll has been set based on the selected radio button in the list. If ActionAll is not a boolean, adjust your list accordingly, to pass the correct type of value for binding to ActionAll.