Question

I have 2 groups of radio buttons with links corresponding to each of the group in my ASP.NET MVC application.

Below is the screen-shot for better clarity:

alt text http://img21.imageshack.us/img21/7959/radiogroups.png

When I select the appropriate link, the value associated with the particular group should be passed on to the controller Action.

For instance in the above case, while clicking "link 1", the values A1 or A2 should passed to the controller action method.

I tried doing a post to the appropriate action method, but the value associated with the second link is also getting passed. How can I get only the selected value from the clicked link?

Was it helpful?

Solution

I would use a javascript function as the href of the link. It would read the selected value of the corresponding radio button group and add it to a url string. You can set the window.location or set the form action and submit.

OTHER TIPS

Using javascript you can clear the values of the other radio buttons before submitting the form.

Other solution can be having a hidden field and populating it with the appropriate value using javascript whenever a link is clicked. Then you get the value of this field and forget about the radiobuttons.

Put the two radio buttons and the associated link into a separate form. Replace the link with a submit button (or submit image if you want it to look like a link). So you will have two forms both posting to the same controller action. Give your submit button a name which can be used inside the action to determine which button was clicked. You could also use a hidden field. The first thing to do in the action is to determine which button was clicked and then read the appropriate request values for the radio buttons.

Another alternative is to have everything in the same form with two submit buttons and different names.

View

@model MvcApplication2.Models.CountryModel
@using (Html.BeginForm())
{
            @Html.RadioButton("Gender", "Male" new { @checked = "checked" }) Male
            @Html.RadioButton("Gender", "Female")Female
            <input type="submit" text="submit"/>
}

MODEL

   public class CountryModel
    {
        public string Gender{ get; set; }

    }

CONTROLLER

public ActionResult Index()
{                    
    return View();
}
[HttpPost]
public ActionResult Index(CountryModel model)
{
    //for radio button
    CountryModel dd = new CountryModel();
    string date1 = model.RDB;
    return View(model);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top