我怎样才能创造出比一个编辑模板的详细在MVC3多行文本框? [英] How can I create more than one editor template for a multi-line textbox in MVC3?

查看:740
本文介绍了我怎样才能创造出比一个编辑模板的详细在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?

此外,我怎么可以声明不同的cols和rows。我可以使用COLS,行或者我应该在 CSS指定高度和宽度 WIDTH = 500像素,高度= 600px的 400像素

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?

推荐答案

您可以覆盖<一个href=\"http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html\">default编辑模板(〜/查看/共享/ 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>

另外,你可以的增强的编辑模板,这样你就不需要在每个指定@class属性EditorFor调用是这样的:

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
) 

现在你可以:

@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" })

哦,不要忘了ID必须是在HTML中唯一的,这样这个 ID =dialogText显然应为第二textarea的不同。

Oh and don't forget that ids must be unique in HTML so this id = "dialogText" should obviously be different for the second textarea.

这篇关于我怎样才能创造出比一个编辑模板的详细在MVC3多行文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆