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?
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; }
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" } })
It looks like this is no longer required on MVC 5 when you have
@Html.EditorFor with DataType.MultilineText field.