asp.net-mvc 如何在 ASP.MVC 中指定多行 Editor-For 的列和行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6217908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I specify the columns and rows of a multiline Editor-For in ASP.MVC?
提问by Dan Sorensen
In ASP.MVC 3, how do I specify the columns and rows for a multiline EditorFor
(textarea)? I am using [UIHint("MultilineText")]
, but can't find any documentation on how to add attributes for the text area.
在 ASP.MVC 3 中,如何指定多行EditorFor
(textarea)的列和行?我正在使用[UIHint("MultilineText")]
,但找不到有关如何为文本区域添加属性的任何文档。
Desired HTML:
所需的 HTML:
<textarea cols="40" rows="10"></textarea>
Relevant Part of my MVC 3 Model:
我的 MVC 3 模型的相关部分:
[UIHint("MultilineText")]
public string Description { get; set; }
Relevant Part of my Razor cshtml:
我的 Razor cshtml 的相关部分:
<div class="editor-field">
@Html.EditorFor(model => model.Description)
</div>
What I'm getting in View Source:
我在查看源代码中得到了什么:
<div class="editor-field">
<textarea class="text-box multi-line" id="Description" name="Description"></textarea>
</div>
How do I set rows and columns?
如何设置行和列?
回答by amit_g
Use TextAreaFor
使用 TextAreaFor
@Html.TextAreaFor(model => model.Description, new { @class = "whatever-class", @cols = 80, @rows = 10 })
or use style for multi-line
class.
或使用样式进行multi-line
类。
You could also write EditorTemplatefor this.
您也可以为此编写EditorTemplate。
回答by Juan Carlos Puerto
In ASP.NET MVC 5 you could use the [DataType(DataType.MultilineText)]
attribute. It will render a TextAreatag.
在 ASP.NET MVC 5 中,您可以使用该[DataType(DataType.MultilineText)]
属性。它将呈现一个TextArea标签。
public class MyModel
{
[DataType(DataType.MultilineText)]
public string MyField { get; set; }
}
Then in the view if you need to specify the rows you can do it like this:
然后在视图中,如果您需要指定行,您可以这样做:
@Html.EditorFor(model => model.MyField, new { htmlAttributes = new { rows = 10 } })
Or just use the TextAreaFor with the right overload:
或者只是使用具有正确重载的 TextAreaFor :
@Html.TextAreaFor(model => model.MyField, 10, 20, null)
回答by Mohammed Dawood Ansari
回答by Khepri
One option seems to be using CSS to style the textarea
一种选择似乎是使用 CSS 来设置 textarea 的样式
.multi-line { height:5em; width:5em; }
See this entry on SOor this one.
Amurra's accepted answer seems to imply this class is added automatically when using EditorFor but you'd have to verify this.
Amurra 接受的答案似乎暗示在使用 EditorFor 时会自动添加此类,但您必须验证这一点。
EDIT: Confirmed, it does. So yes, if you want to use EditorFor, using this CSS style does what you're looking for.
编辑:确认,确实如此。所以是的,如果您想使用 EditorFor,使用此 CSS 样式可以满足您的需求。
<textarea class="text-box multi-line" id="StoreSearchCriteria_Location" name="StoreSearchCriteria.Location">
回答by CyberNinja
in mvc 5
在 mvc 5
@Html.EditorFor(x => x.Address,
new {htmlAttributes = new {@class = "form-control",
@placeholder = "Complete Address", @cols = 10, @rows = 10 } })
回答by Cobblestone1
In .net VB- you could achieve control over columns and rows with the following in your razor file:
在.net VB 中- 您可以在 razor 文件中使用以下内容控制列和行:
@Html.EditorFor(Function(model) model.generalNotes, New With {.htmlAttributes = New With {.class = "someClassIfYouWant", .rows = 5,.cols=6}})
回答by Burhan Kaya
@Html.TextArea("txtNotes", "", 4, 0, new { @class = "k-textbox", style = "width: 100%; height: 100%;" })
@Html.TextArea("txtNotes", "", 4, 0, new { @class = "k-textbox", style = "width: 100%; height: 100%;" })