【问题标题】:ASP.NET MVC User InterfaceASP.NET MVC 用户界面
【发布时间】:2015-03-11 05:06:29
【问题描述】:

我有复选框 UI 的问题。当我将它与@Html.EditorFor 一起使用时,它是不正确的。它仅在 html 中正确显示,这意味着 html 控件在 UI 中正确显示,但是当我将 css 类与 @Html.EditorFor 一起使用时,UI 没有正确显示。

查看

@using (Html.BeginForm("Create", "Member", FormMethod.Post, new { id = "Create" }))
{
  <div class="form-group">
    @Html.LabelFor(model => model.Member_Temple, new { @class = "control-label col-md-2" })
    <div class="col-md-10 col-sm-9">
      @Html.EditorFor(model => model.Member_Temple, new { @class = "checkbox form-control" })
    </div>
  </div>
  <div class="form-group ">
    <label for="agree" class="control-label col-md-2">Temple</label>
    <div class="col-lg-10 col-sm-9">
      <input type="checkbox" style="width: 20px" class="checkbox form-control" id="agree" name="agree" />
    </div>
  </div>
}

型号

[DisplayName("Temple")]
public bool Member_Temple { get; set; }

查看页面源代码

问题是 style="width: 20px" 被添加到 html 源中,但不是 @Html...

使用@Html.EditorFor@Html.CheckboxFor 编码

<input data-val="true" data-val-required="The Temple field is required." htmlattributes="{ class = checkbox form-control }" id="Member_Temple" name="Member_Temple" type="checkbox" value="true">

仅包含 html 的代码

<input type="checkbox" style="width: 20px" class="checkbox form-control" id="tmp" name="tmp">

【问题讨论】:

  • 为什么不使用@Html.CheckBoxFor()
  • 您使用的是哪个版本的 MVC? (低于 5.1,您不能传递 html 属性,对于 5.1+,语法为 @Html.EditorFor(m=&gt; m, new { htmlAttributes = new { @class = "form-control" }, })
  • 感谢您的回复。我试过@html.CheckBoxFor 但没有用。
  • but didn't work 是我们最喜欢的解释。
  • 如果@html.CheckBoxFor() 不起作用,那么您没有正确使用它。显示您尝试过的内容。

标签: c# css asp.net asp.net-mvc razor


【解决方案1】:

为什么不使用CheckBoxFor Method?在您的使用中,它看起来像:

@Html.CheckBoxFor(model => model.Member_Temple, new { @class = "checkbox form-control" })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-07
    • 2013-03-25
    • 1970-01-01
    • 2010-09-22
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多