17
    @Html.RadioButtonFor(Model => Model.Location, "Location")
    @Html.LabelFor(Model=>Model.Location,"Location")
    @Html.RadioButtonFor(Model=>Model.Model,"Model")
    @Html.LabelFor(Model=>Model.Model,"Model")
    @Html.RadioButtonFor(Model=>Model.MovableUnit,"MU")
    @Html.LabelFor(Model=>Model.MovableUnit,"MU")




   <input id="Location" name="Location" type="radio" value="Location" />
    <label for="Location">Location</label>
    <input id="Model" name="Model" type="radio" value="Model" />
    <label for="Model">Model</label>
    <input id="MovableUnit" name="MovableUnit" type="radio" value="MU" />
    <label for="MovableUnit">MU</label>

How to have a common name="radiobtn" for all the above Radio buttons? The problem is I want to select only one radio button at a time, but in this case all are selectable at the same time.

2
  • 1
    @Html.RadioButtonFor(Model => Model.Location, "LOC",new { @Name="Location"}) @Html.RadioButtonFor(Model => Model.Model_Number, "MOD", new { @Name="Location"}) @Html.RadioButtonFor(Model => Model.MovableUnit, "MU", new { @Name="Location"}) Commented Jun 2, 2014 at 13:28
  • I have three controller parameters Location,Model_Number,MovableUnit.Which one to use as Name attribute?? Commented Jun 2, 2014 at 13:33

2 Answers 2

32

Just create a dummy property in your Model class like, public string Dummy{get; set;}

@Html.RadioButtonFor(Model => Model.Location, "LOC", new { @Name = "Dummy"})
@Html.RadioButtonFor(Model => Model.Model_Number, "MOD", new { @Name = "Dummy" })
@Html.RadioButtonFor(Model => Model.MovableUnit, "MU", new { @Name = "Dummy" })

Get the value "LOC", "MOD", "MU" using property name 'Dummy'.

Sign up to request clarification or add additional context in comments.

3 Comments

Note... @Name is case sensitive.
This works, but it's very hacky. If you check the source, you'll see there are now 2 attributes, "Name" and "name". It's just a browser quirk that "Name" happens to take precedence.
Having two same attributes means if they ever change how browsers behave, this will stop working or start missbehaving
5

Use the function without "for", then you can specify the name:

@Html.RadioButton("newname", "value", new { @id="oldname" })

5 Comments

I tested it and it works. Make sure you are using RadioButton(..) instead of RadioButtonFor(..), and that you are passing a string as the name parameter in the function.
I'm afraid you can't do that. Check this out: stackoverflow.com/questions/10716468/…
@Html.RadioButtonFor(Model => Model.Location, "LOC", new { @Name = "n1" }) @Html.LabelFor(Model => Model.Location, "Location") Doing so for three different radio button,only 1 is selectable but the value is not passed to controller from view.
Make sure your controller's parameter name matches the name you are giving (n1)
Dude ! But my other Controller parameters are getting the same value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.