Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

asp.net mvc - How can I create more than one editor template for a multi-line textbox in MVC3?

I have the following two types of multiline textareas in my views:

<textarea cols="100" rows="15" class="full-width" id="dialogText" 
          name="Text">@Model.Text</textarea>

<textarea cols="100" rows="10" class="full-width" id="dialogText" 
          name="Text">@Model.Text</textarea> 

I would like to take advantage of custom editor templates, but keep the ability to specify attributes differently (e.g. the rows are different between the two above).

Is it possible for me to declare and use two different kinds of templates for the same field? If so then how should I declare the template and how do I specify the different templates to use?

Also how can I declare the different cols and rows. Can I use cols,rows or should I specify height and width in CSS such as width=500px, height=600px or 400px?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...