从 Viewmodel 访问类型化视图中的列表属性
我有一个主视图模型,以下两个列表来自两个不同的数据库表,
public IEnumerable <Customer> SomeCustomer { get; set; }
public IEnumerable <CustomerSite> CustomerSites { get; set; }
在我的控制器中,我有
public ViewResult Index()
{
masterViewModel sitesModel = new masterViewModel();
return View(sitesModel);
}
然后在我的视图中,我写
@model IEnumerable<trsDatabase.Models.masterViewModel>
如果我尝试通过使用每个语句访问模型
@foreach (var customer in Model)
当我输入@customer时。它只会让我访问两个列表,而不是列表中的各个属性。
我以为我可以用例如来做到这一点
@customer.CustomerSites.UnitNo
但是我能走的最远的是
@customer.CustomerSites
这显然不会让我访问列表中的各个属性
有人对我在这里做错了什么有任何想法吗?我需要在控制器中的视图模型中定义属性吗? (我认为不是)
更新
我的视图模型中有以下代码,
namespace trsDatabase.Models
{
public class masterViewModel
{
public IEnumerable <Customer> Customers { get; set; }
public IEnumerable <CustomerSite> CustomerSites { get; set; }
}
}
然后控制器中有以下代码,
public ViewResult Index()
{
masterViewModel sitesModel = new masterViewModel();
return View(sitesModel);
}
然后我将视图声明更改为
@model trsDatabase.Models.masterViewModel
这允许我完美地从视图访问属性,但它引发 foreach 循环错误,因为当视图继承模型而不是 Ienumerable 列表时无法使用它,我是否需要更改在视图中渲染列表的语法并使用 foreach 循环以外的其他内容?
I have a master view model, with the following two lists that are from two different DB tables,
public IEnumerable <Customer> SomeCustomer { get; set; }
public IEnumerable <CustomerSite> CustomerSites { get; set; }
In my controller I have
public ViewResult Index()
{
masterViewModel sitesModel = new masterViewModel();
return View(sitesModel);
}
Then in my view I write
@model IEnumerable<trsDatabase.Models.masterViewModel>
If I try to access the model by using a for each statement
@foreach (var customer in Model)
When I type @customer. It will only let me access the two lists not the individual properties in the list.
I thought I would have been able to do this with e.g.
@customer.CustomerSites.UnitNo
But the furthest i can go is
@customer.CustomerSites
And this obviously will not let me access the individual properties in the lists
Does anyone have any ideas on what I'm doing wrong here? Do I need to define the properties from the viewmodel in the controller? (I think not)
Update
I have the following code in my viewmodel,
namespace trsDatabase.Models
{
public class masterViewModel
{
public IEnumerable <Customer> Customers { get; set; }
public IEnumerable <CustomerSite> CustomerSites { get; set; }
}
}
Then the following in the controller,
public ViewResult Index()
{
masterViewModel sitesModel = new masterViewModel();
return View(sitesModel);
}
Then I changed my view declaration to
@model trsDatabase.Models.masterViewModel
This allows me to access the properties from the view perfectly, but it throws an error with the foreach loop as it can't be used when the view is inheriting the model rather than the Ienumerable list, do I need to change the syntax for rendering the list in the view and use something other than a foreachloop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要另一个循环,因为 CustomerSites 是一个集合:
在评论后编辑
创建一个 ViewModel 类:
将其填充到控制器中并将其传递给
View()
函数。更改要输入的视图 (
@model ViewModel
)。然后在视图中,您的
Model
属性将是ViewModel
类型,您可以通过@Model.Customers
访问客户集合I think you need another loop, as CustomerSites is a collection:
EDIT AFTER COMMENT
Create a ViewModel class:
Populate it in the controller and pass it to
View()
function.Change the view to be typed (
@model ViewModel
).Then inside view your
Model
property will be of typeViewModel
and you can access customers collection through@Model.Customers