【问题标题】:@Html.CheckboxFor not displaying in mvc@Html.CheckboxFor 不在 mvc 中显示
【发布时间】:2016-03-23 23:14:52
【问题描述】:

复选框未显示在页面中。我在谷歌尝试了很多解决方案。没有任何效果。代码如下:

@model project.gamestatus

@using (Html.BeginForm("Create", "Calculator", FormMethod.Post, new { id = "frmID" }))
{

        //other codes

        <div class="form-group">
                @Html.Label("Show on Screen", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.CheckBoxFor(m => m.display_status)
                    @Html.ValidationMessageFor(model => model.display_status, "", new { @class = "text-danger" })
                </div>
            </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-info" />
            </div>
        </div>
}

只有当复选框显示在页面中时,我才能继续进行验证。在我看来,没有复选框

【问题讨论】:

  • 您尚未在复选框上设置值。在这里查看我的答案stackoverflow.com/questions/22174475/…
  • 没有显示是什么意思?您有一个CheckBoxFor() 方法,该方法将为您可以通过检查页面源代码轻松验证的属性生成html(&lt;input type="checkbox" .. /&gt;&lt;input type="hidden" ... /&gt;)。如果您没有直观地看到它,那么您的 css 可能有问题。
  • @Stephen Muecke 当我检查 html 代码时,我发现它有一个 css 属性“display:none”。所以复选框没有显示在视图中

标签: asp.net-mvc


【解决方案1】:

首先,您需要确保 display_status 模型是布尔类型,并为它分配显示名称以及任何验证。

[Display(Name="CheckBox Display Name")]
[Required]
public bool display_status { get; set; }

另外,@Html.CheckBoxFor 不支持复选框标签。因此,您可以使用@Html.LabelFor 获得复选框的标签,如下所示:

<div class="form-group">
    @Html.Label("Show on Screen", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.CheckBoxFor(m => m.display_status)
        @Html.LabelFor(m => m.display_status)
        @Html.ValidationMessageFor(m => m.display_status, "", new { @class = "text-danger" })
    </div>
</div>

【讨论】:

  • 我签入了检查器,它总是有一个由助手自动生成的 CSS 属性“display:none”
【解决方案2】:

虽然这是一篇很老的帖子,但我遇到了这个问题,但是当我检查时发现 CSS 属性 opacity 设置为 0,因此将其更改为 1 将解决问题

 @Html.EditorFor(model => model.Active, new { htmlAttributes = new { style = "opacity: 1" } })

【讨论】:

    【解决方案3】:

    我通过将高度明确设置为 20px 来解决问题。当我检查页面时,它被设置为 20%:

    @Html.CheckBoxFor(m => m.ExistingUser, new { style="height:20px" })

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      相关资源
      最近更新 更多