Question

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?

Était-ce utile?

La solution

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 of true and false, then if your controller action has a boolean variable named ActionAll, 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:

[HttpPost]
public ActionResult TemplateInfo(bool ActionAll, ... )

Then you can pass the ActionAll variable from a button list in your view with the following:

<input type="radio" value="true" id="ActionAll" name="ActionAll" /> All
<input type="radio" value="false" selected="selected" id="ActionAll" name="ActionAll" /> Not All

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top