Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to keep distinct name for each radio button
    primarykey
    data
    text
    <p>I have an MVC3 Internet Application.</p> <p>Basically it has a registration form, which has 3 radio buttons on it. Whenever i check one of the radio button and hit <code>submit</code> button, it should go to the controller and insert data into my database.</p> <p>I also have validations to the fields in my <code>Registration</code> form.</p> <p>Somewhere in <strong><code>stackoverflow</code></strong>, i've read that <code>Property names in the Model and the Names of the fields in the view have to be matched</code> inorder to preserve the values in the form even after the validation errors.</p> <p>This statement works perfectly for TextBoxes and for Dropdownlists.</p> <p><strong>My Question is :</strong> How to have distinct name foreach <code>radiobutton</code> ?</p> <p>As I've said my form has 3 radio buttons.</p> <p>Say,These are the TextBoxes followed by radio buttons </p> <pre><code>Phone 1 // here is the radio button1 Phone 2 // here is the radio button2 Phone 3 // here is the radio button3 </code></pre> <p>In my model i've </p> <pre><code>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; } </code></pre> <p>I've tried something like this</p> <pre><code>Phone1: @Html.RadioButtonFor(model =&gt; model.PreferredPhone, Model.Phone1, new { id = "Phone1", @onclick = "PrefferedPhone(this)" }) Phone2: @Html.RadioButtonFor(model =&gt; model.PreferredPhone, Model.Phone2, new { id = "Phone2", @onclick = "PrefferedPhone(this)" }) Phone3: @Html.RadioButtonFor(model =&gt; model.PreferredPhone, Model.Phone3, new { id = "Phone3", @onclick = "PrefferedPhone(this)" }) </code></pre> <p>but this declaration sets the name of all the radio buttons to <code>PreferredPhone</code> </p> <p>How to have a different name for each Radio Button ?</p> <p>Even i tried by keeping RadioButton names as <code>model=&gt;model.Phone1</code>,<code>model=&gt;model.Phone2</code>,<code>model=&gt;model.Phone3</code>, but if i set like this i can be able to select all the three radio buttons.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload