如何重写“名称”.Razor的html属性

本文关键字:Razor html 属性 名称 何重写 重写 | 更新日期: 2023-09-27 18:05:38

    @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"为所有上述单选按钮?问题是我想一次只选择一个单选按钮,但在这种情况下,所有的都是可选择的。

如何重写“名称”.Razor的html属性

在Model类中创建一个虚拟属性,如下所示:公共字符串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" })

使用属性名'Dummy'获取值"LOC", "MOD", "MU"

使用不带"for"的函数,然后可以指定名称:

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