You could override the default editor template (~/Views/Shared/EditorTemplates/MultilineText.cshtml
):
@Html.TextArea(
"",
ViewData.TemplateInfo.FormattedModelValue.ToString(),
ViewData
)
and then assuming you have defined a view model:
public class MyViewModel
{
[DataType(DataType.MultilineText)]
public string Text { get; set; }
}
inside the main view you could do this:
@model MyViewModel
@Html.EditorFor(x => x.Text, new { cols = "100", rows = "15", id = "dialogText", @class = "full-width" })
@Html.EditorFor(x => x.Text, new { cols = "100", rows = "10", id = "dialogText", @class = "full-width" })
which will render the expected output:
<textarea class="full-width" cols="100" id="dialogText" name="Text" rows="15">
hello world
</textarea>
<textarea class="full-width" cols="100" id="dialogText" name="Text" rows="10">
hello world
</textarea>
Also you could enhance the editor template so that you don't need to specify the @class attribute at every EditorFor call like this:
@{
var htmlAttributes = ViewData;
htmlAttributes["class"] = "full-width";
}
@Html.TextArea(
"",
ViewData.TemplateInfo.FormattedModelValue.ToString(),
htmlAttributes
)
and now you could:
@model MyViewModel
@Html.EditorFor(x => x.Text, new { cols = "100", rows = "15", id = "dialogText" })
@Html.EditorFor(x => x.Text, new { cols = "100", rows = "10", id = "dialogText" })
Oh and don't forget that ids must be unique in HTML so this id = "dialogText"
should obviously be different for the second textarea.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…