简体   繁体   English

LabelFor + EditorFor 在同一行?

[英]LabelFor + EditorFor on the same line?

I have this code:我有这段代码:

<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>

How do I get the label and checkbox(in this case its a checkbox) on the same line?如何在同一行上获取 label 和复选框(在本例中为复选框)? I have tried removing the divs and they still come out on different lines.我试过删除 div,但它们仍然出现在不同的行上。

Thank you.谢谢你。

Removing the didn't work for my app, but adding new { @style = "display:inline-block" } as the second argument on the LabelFor() did. 删除对我的应用程序不起作用,但添加new { @style = "display:inline-block" }作为LabelFor()的第二个参数。

I prefer the checkbox before the label so it would be: 我更喜欢标签前的复选框,所以它会是:

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

Just remove the divs separating them, this works for me, I get Ticket [] with this. 只需删除分隔它们的div,这对我有用,我得到Ticket []

Also, use CheckBoxFor if you know it is a CheckBox 此外,使用CheckBoxFor如果你知道它是一个CheckBox

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

I have also used this code as the OP stated this is his code 我也使用了这个代码作为OP声明这是他的代码

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

I get GetAll [] 我得到GetAll []

GetAll is a bool in my ViewModel GetAll是我的ViewModel中的一个bool

Also used this 也用这个

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

I get GetAll [] 我得到GetAll []

And this 和这个

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

I get GetAll [] 我得到GetAll []

In every case my tests are label and checkbox are inline 在每种情况下,我的测试都是标签,复选框是内联的

In my case just add the row to the main div because I want to use the col-md-2在我的例子中,只需将行添加到主 div,因为我想使用 col-md-2

classes so after searching for solution when use col-*类所以在使用 col-* 搜索解决方案之后

classes then the parent element needs the row classes: classes 那么父元素需要行类:

it was without row class它没有行 class

<div class="form-group ">

and just add row class:只需添加行 class:

 <div class="form-group row">
            @Html.LabelFor(model => model.BATCH_NO, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-3">
                @Html.EditorFor(model => model.BATCH_NO, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.BATCH_NO, "", new { @class = "text-danger" })
            </div>
        </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM