【问题标题】:LabelFor + EditorFor on the same line?LabelFor + EditorFor 在同一行?
【发布时间】:2012-06-03 05:20:08
【问题描述】:

我有这个代码:

<div class="editor-label">
    @Html.LabelFor(model => model.Ticket)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Ticket)
    @Html.ValidationMessageFor(model => model.Ticket)
</div>

如何在同一行获取标签和复选框(在本例中为复选框)?我已经尝试删除 div,但它们仍然出现在不同的行上。

谢谢。

【问题讨论】:

    标签: asp.net .net asp.net-mvc asp.net-mvc-3 razor


    【解决方案1】:

    删除不适用于我的应用,但添加 new { @style = "display:inline-block" } 作为 LabelFor() 的第二个参数可以。

    我更喜欢标签前的复选框,所以它是:

    <div>
        @Html.EditorFor(model => model.GetAll)
        @Html.LabelFor(model => model.GetAll, new { @style = "display:inline-block" })
    </div>
    

    【讨论】:

      【解决方案2】:

      只需删除分隔它们的 div,这对我有用,我得到Ticket []

      另外,如果您知道CheckBoxForCheckBox,请使用CheckBox

      <div>
          @Html.LabelFor(model => model.Ticket)
          @Html.CheckBoxFor(model => model.Ticket)
          @Html.ValidationMessageFor(model => model.Ticket)
      </div>
      

      我也使用了这个代码,因为 OP 说这是他的代码

      <div class="editor-field">
          @Html.Label("SMS Alerts?")
          @Html.EditorFor(model => model.GetAll)
      </div>
      

      我收到GetAll []

      GetAll 在我的 ViewModel 中是 bool

      也用过这个

      <div>
          @Html.LabelFor(model => model.GetAll)
          @Html.EditorFor(model => model.GetAll)
      </div>
      

      我收到GetAll []

      还有这个

      <div class="editor-field">
          @Html.LabelFor(model => model.GetAll)
          @Html.EditorFor(model => model.GetAll)
      </div>
      

      我收到GetAll []

      在每种情况下,我的测试都是标签和复选框是内联的

      【讨论】:

      • 我刚刚重试为:&lt;div class="editor-field"&gt; @Html.Label("SMS Alerts?") @Html.EditorFor(model =&gt; model.SmsAlerts) &lt;/div&gt; 并且它们之间有一个换行符。我不知道为什么这会发生在我身上=/。
      • 我已经用那个代码试过了,我仍然可以很好地内联。这些字段是否包含在可能设置样式的其他元素中?
      • 我使用了&lt;div class="editor-field"&gt; @Html.Label("SMS Alerts?") @Html.EditorFor(model =&gt; model.GetAll) &lt;/div&gt;,其中 GetAll 在我看来是一个布尔字段。我得到SMS Alerts? []
      • 您对另一个元素的评论帮助我找到了来源。如果我删除所有输入周围的
        标记,此行为就会消失。你知道为什么
        标签会导致这种情况发生吗?谢谢你的帮助。
      • 字段集仅用于将相关元素组合在一起,它在它们周围画一条线,就像一个组合框。也许那个 Fieldset 是因为宽度可能太窄而强制包裹元素。
      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      相关资源
      最近更新 更多