我是初学者 ASP.NET MVC 3 Web 开发人员并且有这个问题:
有几种视图模型,它们具有相似的逻辑,我想到有一个通用的 EditorTemplate 供它们由Html.EditorFor
.
该模板被命名为“ ExistOrCreateNewInput.cshtml
”,并且是带有IExistOrCreateNewInput
接口类的强类型:
public interface IExistOrCreateNewInput
{
int? existEntId { get; set; }
IUnapprovedNewEntityCreateInput createInput { get; set; }
}
模板的内容类似于:
@model IExistOrCreateNewInput
<h2>Add or choose</h2>
@* here put some common js code *@
Html.EditorFor(o => o.existEntId)
Html.EditorFor(o => o.createInput)
假设我有某种实现此接口的模型,例如:
public class FirstModelInput : IExistOrCreateNewInput
{
[Display(Name="First")]
[UIHint("Lookup")]
public int? existEntId {get; set;}
[UIHint("PaperCreateInput")]
public PaperCreateInput paperCreateInput {get; set;}
public IUnapprovedNewEntityCreateInput createInput
{
get
{
return paperCreateInput;
}
set { }
}
}
public class SecondModelInput : IExistOrCreateNewInput
{
[Display(Name="Second")]
[UIHint("Lookup")]
public int? existEntId {get; set;}
[UIHint("ThesisCreateInput")]
public ThesisCreateInput thesisCreateInput {get; set;}
public IUnapprovedNewEntityCreateInput createInput
{
get
{
return thesisCreateInput;
}
set { }
}
}
public class ThirdModelInput : IExistOrCreateNewInput
{
...
}
wherePaperCreateInput
和ThesisCreateInput
classes 实现IUnapprovedNewEntityCreateInput
接口。所以,我想要我的视图模型
public class SomeGlobalViewModel
{
[Required]
string name {get; set;}
[UIHint("ExistOrCreateNewInput")]
FirstModelInput firstModel {get; set;}
}
用Html.EditorFor(o => o.firstModel)
方法正确渲染属性“firstModel”。现在我知道 EditorFor 方法正在处理元数据,所以我的问题可能应该是“如何将属性的元数据传递给基本接口属性”。如我错了请纠正我。
无论如何,我需要那些辅助方法
Html.EditorFor(o => o.existEntId)
Html.EditorFor(o => o.createInput)
在我的编辑器模板(带有接口的强类型)中呈现我的模型属性,因为我在实现类中声明它们:
[UIHint("Lookup")]
public int? existEntId {get; set;}
[UIHint("PaperCreateInput")]
public PaperCreateInput createInput {get; set;}
提前致谢。对不起,我的英语不好。