Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override the "name" HtmlAttribute of Razor

    @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.

like image 684
Vivek Ranjan Avatar asked May 28 '14 12:05

Vivek Ranjan


People also ask

How do I change my razor view ID?

Just add the id property to the html-attributes. That will override the default id generated by the editorfor-helper-methode.

What is HTML EditorFor?

The Html. Editor() or Html. EditorFor() extension methods generate HTML elements based on the data type of the model object's property. The following table list the data types and releted HTML elements: DataType.

What is HTML HiddenFor?

HiddenFor() is a strongly typed method that is bounded with model class. It communicates and send/receive value to model class properties. Generally it contains 2 parameters; Hidden Field Name which is a model property and Value for Hidden Field.


1 Answers

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'.

like image 101
Vivek Ranjan Avatar answered Sep 23 '22 09:09

Vivek Ranjan