如何在同一视图中为基本 ViewModel 和派生 ViewModel 使用两个不同的 EditorTemplate?
我有很多从基本 ViewModel 派生的 ViewModel。
是否可以在同一个视图中为来自基本 ViewModel 的派生 ViewModel 部分显示 EditorTemplate,并为派生部分显示不同的模板?如果是,是如何完成的?
基础 ViewModel:
public class ShowQuestionViewModel
{
public int Question_ID { get; set; }
public String Question_Wording { get; set; }
public String Question_Type { get; set; }
public String Question_Number { get; set; }
public Boolean Visible { get; set; }
public Boolean IsAnswered { get; set; }
}
派生 ViewModel:
public class ShowMatrixQuestionViewModel : ShowQuestionViewModel
{
public Dictionary<MatrixRows, List<MatrixColumns>> columnrow;
public List<MatrixColumns> columns;
public List<MatrixRows> rows;
public ShowMatrixQuestionViewModel()
{
columns = new List<MatrixColumns>();
rows = new List<MatrixRows>();
columnrow = new Dictionary<MatrixRows, List<MatrixColumns>>();
}
}
public class MatrixColumns
{
public int Column_ID { get; set; }
public int Column_Number { get; set; }
public String Column_Description { get; set; }
public Boolean IsAnswer { get; set; }
}
public class MatrixRows
{
public int Row_Id { get; set; }
public String Row_Number { get; set; }
public String Row_Description { get; set; }
}
因此,当我使用 EditorFor(x => ShowMatrixQuestionViewModel) 时,我想对来自 ShowQuestionViewModel 的属性使用特殊编辑器。
I have a lot of ViewModels that derive from a base ViewModel.
Is it possible to display an EditorTemplate for the part of the derived ViewModel that comes from the base ViewModel, and a different template for the derived part, all in the same View? If yes, how is it done?
Base ViewModel:
public class ShowQuestionViewModel
{
public int Question_ID { get; set; }
public String Question_Wording { get; set; }
public String Question_Type { get; set; }
public String Question_Number { get; set; }
public Boolean Visible { get; set; }
public Boolean IsAnswered { get; set; }
}
Derived ViewModel:
public class ShowMatrixQuestionViewModel : ShowQuestionViewModel
{
public Dictionary<MatrixRows, List<MatrixColumns>> columnrow;
public List<MatrixColumns> columns;
public List<MatrixRows> rows;
public ShowMatrixQuestionViewModel()
{
columns = new List<MatrixColumns>();
rows = new List<MatrixRows>();
columnrow = new Dictionary<MatrixRows, List<MatrixColumns>>();
}
}
public class MatrixColumns
{
public int Column_ID { get; set; }
public int Column_Number { get; set; }
public String Column_Description { get; set; }
public Boolean IsAnswer { get; set; }
}
public class MatrixRows
{
public int Row_Id { get; set; }
public String Row_Number { get; set; }
public String Row_Description { get; set; }
}
So, when i use EditorFor(x => ShowMatrixQuestionViewModel), i want to use a special editor for the properties that come from ShowQuestionViewModel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个单独的编辑器模板:
然后定义一个自定义编辑器模板
~/Views/Shared/EditorTemplates/ShowMatrixQuestionViewModel.cshtml
,您可以在其中自定义所有属性,包括基本类型的属性。You could create a separate editor template:
and then define a custom editor template
~/Views/Shared/EditorTemplates/ShowMatrixQuestionViewModel.cshtml
where you could customize all properties including those for the base type.