【问题标题】:Convert html to checkbox razor view将 html 转换为复选框剃刀视图
【发布时间】:2018-12-11 18:19:43
【问题描述】:

我有以下复选框:

<input type="checkbox" id="seaMode" name="transportMode" value="Origin Service" />
<label for="seaMode" data-label="Sea">Sea</label>

但是我使用剃刀视图将视图绑定到模型:

@foreach (var val in Model.TransportList)
{
    @Html.Label(val.Value, htmlAttributes: new { @for = label })
    <i class=@val.Image></i>
    @Html.CheckBox(label, false, new { value = @val.Text, name = "transportMode" })
}

现在,它返回如下:

<label for="SEAMode">SEA</label>
<i class="icon-sea"></i>
<input id="SEAMode" name="SEAMode" type="checkbox" value="1">
<input name="SEAMode" type="hidden" value="false">

任何一位擅长html的人都可以帮我添加正确的类和属性,以便将html呈现为第一个吗?

对于信息,val.Value 有 SEA 和 val.Text = 1 等等。

【问题讨论】:

  • 所以您只想将类名和数据属性添加到@Html.CheckBox(label, false, new { value = @val.Text, name = "transportMode" })
  • @M12Bennett:是的

标签: html razor checkbox


【解决方案1】:

如果您尝试添加类和数据属性,那么应该这样做:

@Html.CheckBox(label, false, new { value = @val.Text, name = "transportMode", @class = "myCheckBox", data_label = "my data label value" })

如果这有帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多