【问题标题】:MVC Editor TemplatesMVC 编辑器模板
【发布时间】:2014-04-10 05:49:26
【问题描述】:

我正在尝试嵌套编辑器模板,但无法正常工作。我的模型结构是

public class CompanyModel
{
    public EmployeeModel Emp { get; set; }
}

public class EmployeeModel
{
    public EmpType EmployeeType { get; set; }
    public string Name { get; set; }
    public DateTime StartDate { get; set; }
    public string EmailAddress { get; set; }
}

public enum EmpType
{
    Manager = 1,
    Assistant = 2,
    TeamLeader = 3
}

每个 EmpType 都有不同的 EditorTemplates 索引视图是

@model CompanyModel

<h3>Index</h3>
@Html.EditorFor(m => m.Emp,"Employee")

然后我创建了 EditorTemplates(Employee.cshtml)

@model EmployeeModel


<h2>Employee</h2>

@Html.EditorForModel("EmpType_"+ Model.EmployeeType.ToString())

EditorTemplate(EmpType_Manager.cshtml)

@model EmployeeModel


<h2>EmpType_Manager</h2>

I am Manager

为了测试,我在控制器中使用虚拟数据填充模型

public ActionResult Index(CompanyModel model)
    {
        EmployeeModel emp = new EmployeeModel
        {
            EmployeeType = EmpType.Manager,
            Name = "xxxx",
            StartDate = DateTime.Now,
            EmailAddress = "xxx@yyy.com"
        };
        model = new CompanyModel();
        model.Emp = emp;
        return View(model);
    }

当我运行它时,它不会调用 EmpType_Manager 模板。有人可以帮我解决这个问题。我尝试过使用局部视图,它有效。但想使用 EditorTemplate 而不是 Partial 视图。

【问题讨论】:

    标签: mvc-editor-templates


    【解决方案1】:

    看起来这是不可能的,因为它是由 mvc 设计的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2015-08-03
      相关资源
      最近更新 更多