![](/img/trans.png)
[英]how can I pass the data using knockoutjs model to mvc viewmodel?
[英]How in MVC can I create a template for data on a model but not the entire model?
我的模型有很多重复的数据结构。 但是我想以相同的方式显示相关数据组。 例如,我在一个文本框旁边有一个复选框,如果该文本框最近被编辑过,则会显示一个箭头。 我不想为相同的条件一遍又一遍地编写相同的代码。 有没有一种方法可以调用视图结构,并传入3个布尔值和一个字符串之类的参数,然后将它们放入正确的html显示模板中。
我正在使用带有C#的MVC5
当然-您应该能够使用显示模板和编辑器模板。 首先,确定可以抽象到独立视图模型中的内容。 根据您的问题,我们可以创建一个具有2个布尔属性和1个字符串属性的独立视图模型。
public class PropertyGroup(){
public bool Checked {get;set;}
public string Text {get;set;}
public bool RecentlyEditied{get;set;}
}
在“共享视图”文件夹中创建一个名为EditorTemplates的文件夹,并将该视图放置在名为PropertyGroup的文件夹内(无论ViewModel类的名称是什么)。 这将是局部视图,看起来像
@model Core.Models.ViewModels.PropertyGroup
<div>
@Html.CheckBoxFor(x=>x.Checked)
@Html.TextBoxFor(x=>x.Text)
@if(Model.RecentlyEdited){
<img src="arrow.gif" />
}
</div>
现在,在任何希望显示该标记的地方,都可以通过调用Html.EditorFor()
帮助器来完成,然后发送该ViewModel类。
@Html.EditorFor(x=>x.PropertyOfTypePropertyGroup)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.