How to override the “name” HtmlAttribute of Razor

2020-06-09 05:44发布

    @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条回答
Root(大扎)
2楼-- · 2020-06-09 06:15

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

@Html.RadioButton("newname", "value", new { @id="oldname" })
查看更多
贪生不怕死
3楼-- · 2020-06-09 06:20

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

查看更多
登录 后发表回答