2011-06-02 65 views
55

在ASP.MVC 3,我如何指定一个多EditorFor(文本域)的行和列?我正在使用[UIHint("MultilineText")],但找不到有关如何为文本区域添加属性的任何文档。如何在ASP.MVC中指定多行编辑器的列和行?

所需的HTML:

<textarea cols="40" rows="10"></textarea> 

我的MVC 3型号的相关部分:

[UIHint("MultilineText")] 
public string Description { get; set; } 

我的剃须刀CSHTML的相关部分:

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

什么我得到的视图来源:

<div class="editor-field"> 
    <textarea class="text-box multi-line" id="Description" name="Description"></textarea> 
</div> 

如何设置行和列?

回答

107

使用TextAreaFor

@Html.TextAreaFor(model => model.Description, new { @class = "whatever-class", @cols = 80, @rows = 10 }) 

或使用样式multi-line类。

你也可以写EditorTemplate这一点。

+0

当然!我想我是在过度思考问题。 – 2011-06-02 17:37:33

5

一种选择似乎使用EditorFor时要使用CSS样式textarea的

.multi-line { height:5em; width:5em; } 

this entry on SOthis one.

Amurra的接受的答案似乎暗示着这个类会自动添加,但你必须验证这一点。

编辑:经证实,它的作用。所以是的,如果你想使用EditorFor,使用这个CSS样式就可以做你想要的。

<textarea class="text-box multi-line" id="StoreSearchCriteria_Location" name="StoreSearchCriteria.Location"> 
+0

谢谢回复原来的海报的问题。当使用一个使用'EditorFor'创建输入字段的框架时,例如_Telerik ASP。NET MVC_,替代'TextAreaFor'不是一个选项。谢谢你。 – Suncat2000 2015-07-15 13:18:59

24

在ASP.NET MVC 5,你可以使用[DataType(DataType.MultilineText)]属性。它将呈现一个TextArea标记。

public class MyModel 
{ 
    [DataType(DataType.MultilineText)] 
    public string MyField { get; set; } 
} 

然后在视图中,如果你需要指定的行,你可以做这样的:用正确的过载

@Html.EditorFor(model => model.MyField, new { htmlAttributes = new { rows = 10 } }) 

或者仅使用TextAreaFor:

@Html.TextAreaFor(model => model.MyField, 10, 20, null) 
+0

很高兴知道他们做出了这样的改变。 :-) – 2014-10-02 20:22:40

+2

目前它的MVC 5,它的工作原理。 – GSerg 2014-11-09 18:14:47

+0

属性'DataType.MultilineText'在MVC 4中也可以工作... – NightElfik 2014-12-10 20:08:56

6

这一个也可以以较少的努力,我相信使用(但我在MVC 5)

@Html.Description(model => model.Story, 20, 50, new { }) 

enter image description here

0

在MVC 5

   @Html.EditorFor(x => x.Address, 
       new {htmlAttributes = new {@class = "form-control", 
        @placeholder = "Complete Address", @cols = 10, @rows = 10 } })