How do I specify the columns and rows of a multiline Editor-For in ASP.MVC?
在ASP.MVC 3中,如何指定多行
所需的HTML:
1 | <textarea cols="40" rows="10"></textarea> |
我的MVC 3模型的相关部分:
1 2 | [UIHint("MultilineText")] public string Description { get; set; } |
我的Razor cshtml的相关部分:
1 | @Html.EditorFor(model => model.Description) |
我所得到的看法来源:
1 | <textarea class="text-box multi-line" id="Description" name="Description"></textarea> |
如何设置行和列?
使用TextAreaFor
1 | @Html.TextAreaFor(model => model.Description, new { @class ="whatever-class", @cols = 80, @rows = 10 }) |
或为
您也可以为此编写EditorTemplate。
在ASP.NET MVC 5中,可以使用
1 2 3 4 5 | public class MyModel { [DataType(DataType.MultilineText)] public string MyField { get; set; } } |
然后在视图中,如果您需要指定行,则可以这样操作:
1 | @Html.EditorFor(model => model.MyField, new { htmlAttributes = new { rows = 10 } }) |
或仅将TextAreaFor与正确的重载一起使用:
1 | @Html.TextAreaFor(model => model.MyField, 10, 20, null) |
我相信可以轻松使用这一功能(但我使用的是MVC 5)
1 | @Html.Description(model => model.Story, 20, 50, new { }) |
一种选择似乎是使用CSS设置文本区域样式
1 | .multi-line { height:5em; width:5em; } |
请参阅SO上的此项或本项。
Amurra接受的答案似乎暗示在使用EditorFor时会自动添加此类,但您必须对此进行验证。
编辑:确认,可以。因此,是的,如果您想使用EditorFor,则使用此CSS样式即可找到所需的内容。
1 | <textarea class="text-box multi-line" id="StoreSearchCriteria_Location" name="StoreSearchCriteria.Location"> |
在.net VB中,您可以使用剃刀文件中的以下内容实现对列和行的控制:
1 | @Html.EditorFor(Function(model) model.generalNotes, New With {.htmlAttributes = New With {.class ="someClassIfYouWant", .rows = 5,.cols=6}}) |
在MVC 5中
1 2 3 | @Html.EditorFor(x => x.Address, new {htmlAttributes = new {@class ="form-control", @placeholder ="Complete Address", @cols = 10, @rows = 10 } }) |