我可以在MVC3中为同一类使用多个编辑器模板吗? [英] Can I use more than one editor template for the same class in MVC3?

查看:74
本文介绍了我可以在MVC3中为同一类使用多个编辑器模板吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个课程:

public class Book
{
public int BookId { get; set; }
public string BookName { get; set; }
public string Description { get; set; }
}

和编辑器模板:

@model MySimpleEditorTemplate.Models.Book

@Html.DisplayFor(p => p.BookId)     @Html.EditorFor(p => p.BookId)
@Html.DisplayFor(p => p.BookName)   @Html.EditorFor(p => p.BookName)
@Html.DisplayFor(p => p.Description)    @Html.EditorFor(p => p.Description)

我可以这样使用编辑器模板:

I can use the editor template like this:

@Html.EditorFor(model => model.Book) 

但是,如果我想拥有两个编辑器模板或两个显示模板,并在同一个班级中使用一个或另一个,该怎么办?这可能吗?

However what if I want to have two editor templates or two display templates and use one or other for the same class? Is this possible?

推荐答案

public static MvcHtmlString EditorFor<TModel, TValue>(
    this HtmlHelper<TModel> html,
    Expression<Func<TModel, TValue>> expression,
    string templateName
)

http://msdn.microsoft.com/en-us/library/ff406506.aspx

如果在控制器的EditorTemplates文件夹中找到名称与templateName参数匹配的模板,则该模板用于呈现表达式.如果在控制器的EditorTemplates文件夹中找不到模板,则Views \ Shared \ EditorTemplates文件夹为搜索与templateName参数名称匹配的模板.如果未找到模板,则使用默认模板."

"If a template whose name matches the templateName parameter is found in the controller's EditorTemplates folder, that template is used to render the expression. If a template is not found in the controller's EditorTemplates folder, the Views\Shared\EditorTemplates folder is searched for a template that matches the name of the templateName parameter. If no template is found, the default template is used."

这篇关于我可以在MVC3中为同一类使用多个编辑器模板吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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