单视图中的多个模型? [英] multiple model in single view ?

查看:62
本文介绍了单视图中的多个模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道我们可以在单个视图中使用多个模型



请通过以下链接:



ASP.NET MVC 4 / MVC 5视图中的多个模型[^]



现在我的问题是

i有3个模型

  public   class  category 
{
public int CatId { get ; set ; }
public string CatName { get ; set ; }
}

public class 子类别
{
public int subcatId { get ; set ; }
public string catid { get ; set ; }
public string subcatname { get ; set ; }
}

public class item
{
public string itemid { get ; set ; }
public int catid { get ; set ; }
public string subcatid { get ; set ; }
public string item { get ; set ; }
}



现在我想要显示

catname,subcatname,itemname

in grid(单个视图中的表格结构



如何在单视图中用多个模型显示它?

解决方案

< blockquote>

  public   class  MyViewModel 
{
public item Item { get ; set ; }
public 类别类别{ get ; set ; }
public 子类别子类别{ get ; set ; }
}


创建一个新的VeiwModel,添加发送到视图所需的属性,

make查询从三个关系填充属性,并将ViewModel发送到视图。



从需要返回值的视图中,执行相反的操作,即将每个返回的字段映射到您的每个关系属性并保存数据。



希望这会有所帮助。


we know that we can use multiple model in single view

please go through this link below:

Multiple Models in a View in ASP.NET MVC 4 / MVC 5[^]

now my question is
i have 3 models

public class category
{
public int CatId { get; set; }
public string CatName { get; set; }
}

public class subcategory
{
public int subcatId { get; set; }
public string catid { get; set; }
public string subcatname { get; set; }
}

public class item
{
public string itemid { get; set; }
public int catid { get; set; }
public string subcatid { get; set; }
public string item { get; set; }
}


now i want to show
catname,subcatname,itemname
in grid(table structure) in single view

how can we show it in single view with multiple model??

解决方案

public class MyViewModel
{
    public item Item { get; set; }
    public category Category { get; set; }
    public subcategory Subcategory { get; set; }
}


Create a new VeiwModel, add the properties that you require to send to your view,
make query to populate the properties from the three relations, and send the ViewModel to the view.

From the view when you need to return the values, do the reverse way, i.e. map each returned field to each your relation properties and save the data.

Hope this will help.


这篇关于单视图中的多个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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