Question

I have an MVC3 Internet Application.

Basically it has a registration form, which has 3 radio buttons on it. Whenever i check one of the radio button and hit submit button, it should go to the controller and insert data into my database.

I also have validations to the fields in my Registration form.

Somewhere in stackoverflow, i've read that Property names in the Model and the Names of the fields in the view have to be matched inorder to preserve the values in the form even after the validation errors.

This statement works perfectly for TextBoxes and for Dropdownlists.

My Question is : How to have distinct name foreach radiobutton ?

As I've said my form has 3 radio buttons.

Say,These are the TextBoxes followed by radio buttons

Phone 1 // here is the radio button1
Phone 2 // here is the radio button2
Phone 3 // here is the radio button3

In my model i've

public string Phone1 { get; set; }
public string Phone2 { get; set; }
public string Phone3 { get; set; }
//also i have a preferred phone--which sets if any of the phone is selected
public bool PreferredPhone { get; set; }

I've tried something like this

Phone1:
@Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone1, new { id = "Phone1", @onclick = "PrefferedPhone(this)" })
Phone2:
@Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone2, new { id = "Phone2", @onclick = "PrefferedPhone(this)" })
Phone3:
@Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone3, new { id = "Phone3", @onclick = "PrefferedPhone(this)" })

but this declaration sets the name of all the radio buttons to PreferredPhone

How to have a different name for each Radio Button ?

Even i tried by keeping RadioButton names as model=>model.Phone1,model=>model.Phone2,model=>model.Phone3, but if i set like this i can be able to select all the three radio buttons.

Was it helpful?

Solution

When you talk about selecting all three radio buttons then it sound like your are talking about them as if they were checkboxes. The radio buttons that are considered in the same group can´t be all selected at the same time, only one selected per group (but perhaps you already know that and I´m misunderstanding).

From what you read somewhere in Stackoverflow that is correct that Property names in the Model and the Names of the fields in the view have to be matched. Fore instance if you have radiobuttons in a view like this:

@Html.RadioButton("stuffRadio", 1)
@Html.RadioButton("stuffRadio", 2)
@Html.RadioButton("stuffRadio", 3)
@Html.RadioButton("stuffRadio", 4)

And then in the controller when the form has been posted:

[HttpPost]
public ActionResult TheAction(Model model, string stuffRadio)
{
    //dostuff to model and other things
    string value = stuffRadio;
    //dostuff to the selected radioStuff :)
}

Anyway, if I´m misunderstang then I want to ask you in return. You say you want to distinct each radiobutton, I´m not quite sure why. Is so you can get all the phone-numbers on the post action and into the controller?

Using my own example I would simply do something like this:

@Html.RadioButton("stuffRadioGroup1", 1)
@Html.RadioButton("stuffRadioGroup2", 2)
@Html.RadioButton("stuffRadioGroup3", 3)
@Html.RadioButton("stuffRadioGroup4", 4)

But then again if I would want to read all thos group selections I would of course have to do the same thing with the parameters into the controller like this:

[HttpPost]
public ActionResult TheAction(Model model, 
    string stuffRadioGroup1, string stuffRadioGroup2, 
    string stuffRadioGroup3, string stuffRadioGroup4)
{
    //dostuff to model and other things
    string value1 = stuffRadioGroup1;
    string value2 = stuffRadioGroup2;
    string value3 = stuffRadioGroup3;
    string value4 = stuffRadioGroup4;
    //dostuff to the selected radioStuff :)
}

Again, may I´m misunderstang and then you of course correct me ;) Later!

########## EDITED ###################

First of all, if you want hidden fields to be submitted they have to be within the form, and the name of that particular hidden field has to be passed as parameter into the controller with the same name. For instance:

@(Html.BeginForm("SomeAction", "Controller")
{
    <input type="hidden" name="derp" value="1234" />
    @Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone3, new { id = "Phone3", @onclick = "PrefferedPhone(this)" })
    <button type="submit">submit</button>
}

If that hidden field is supposed to be pass as well, the controller should be something like this:

[HttpPost]
public ActionResult SomeAction(Model mymodel, string derp, FormCollection collection)
{
    int number = Convert.toInt32(derp);
    //now number should be '1234'

    string phone3 = collection.Get("Phone3"); 
    //now you have the phone number and the hidden field telling you if that number is preffered

    TryUpdateModel(mymodel); //populate the model if it hasn´t already been populated

    if(ModelState.IsValid)
    {
        //do stuff
    else
    {
        //TODO: here you should set the model with the stuff from derp and collection before returning
        return View("SameViewAsBefore", mymodel);
    }
}

As you can see I put FormCollection in there, just to have a different approach. Hope this helps, if not, you can always ask again :) (I´m known to misunderstand things sometimes)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top