Using Html.EditorFor to generate a textarea with a

2019-06-16 11:03发布

I know you can use the DataType attribute with the EditorFor html helper to specify that a particular property of a model entity should be displayed as a multi-line input field.

What If I want to specify the number of rows and columns the text area must have?

In the model :

[DataType(DataType.MultilineText)]
public string HTMLText { get; set; }

In the view :

@Html.EditorFor(x => x.HTMLText)

Wanted result :

<textarea id="HTMLText" rows="10" cols="40">value</textarea>

Is there a way to generate this kind of code without using the @Html.Textarea() helper?

3条回答
贪生不怕死
2楼-- · 2019-06-16 11:27

You can specify the attributes size and char equivalent of row and col in Razor/c# ie:

@Html.TextAreaFor(model => model.Longtext, new { htmlAttributes = new { @class = "ctrl-col2 ctrl-col1", @row = "4", @col = "30" } })
查看更多
beautiful°
3楼-- · 2019-06-16 11:41

It looks like this is no longer required on MVC 5 when you have @Html.EditorFor with DataType.MultilineText field.

查看更多
乱世女痞
4楼-- · 2019-06-16 11:42

Not sure how to set the rows and cols, but you can alter the CSS of those textareas using the .multi-line class. This class is added to the textarea when using EditorFor so you can specify the width and height in that class to get your desired dimensions.

.multi-line { height:5em; width:5em; }
查看更多
登录 后发表回答