如何覆盖Razor的“名称”HtmlAttribute

时间:2014-05-28 12:57:08

标签: c# asp.net-mvc-4 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>

如何为所有上述单选按钮设置一个通用名称=“radiobtn”?         问题是我想一次只选择一个单选按钮,但在这种情况下,所有单选按钮都可以同时选择。

2 个答案:

答案 0 :(得分:23)

只需在Model类中创建一个虚拟属性,就像 public string Dummy {get;设置;}

@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" })

获取价值&#34; LOC&#34;,&#34; MOD&#34;,&#34; MU&#34;使用属性名称&#39; Dummy&#39;。

答案 1 :(得分:3)

使用不带&#34;的功能&#34;,然后您可以指定名称:

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