如何制作 IEnumerable 类型的 Viewmodel?
我将以下代码用于包含两个数据列表的主视图模型,
namespace trsDatabase.Models
{
public class masterViewModel
{
public IEnumerable <Customer> Customers { get; set; }
public IEnumerable <CustomerSite> CustomerSites { get; set; }
}
}
我使用以下代码将 veiwmodel 传递给视图,
public ViewResult Index()
{
masterViewModel sitesModel = new masterViewModel();
return View(sitesModel);
}
然后在我看来,我有以下内容,
@model IEnumerable<trsDatabase.Models.masterViewModel>
foreach (var site in customer.CustomerSites)
{
foreach (var cust in customer.Customers)
{
<tr>
<td>
@cust.CustomerName
</td>
<td>
@site.UnitNo
</td>
使用上面的代码我可以访问视图模型中两个列表中的所有属性,但是当我导航到视图时,我收到错误,因为视图需要 IEnumerable。如果我更改声明以仅传递视图模型,
@model trsDatabase.Models.masterViewModel
则 foreach
语句将不起作用,它会给出此错误
传递到字典中的模型项的类型为“trsDatabase.Models.masterViewModel”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[trsDatabase.Models.masterViewModel]”的模型项。< /p>
任何人都可以提供任何建议或为我指出解决此问题的正确方向,是否可以使我的视图模型 IEnumerable?
I am using the following code for a master view model that contains two lists of data,
namespace trsDatabase.Models
{
public class masterViewModel
{
public IEnumerable <Customer> Customers { get; set; }
public IEnumerable <CustomerSite> CustomerSites { get; set; }
}
}
I am using the following code to pass the veiwmodel to the view,
public ViewResult Index()
{
masterViewModel sitesModel = new masterViewModel();
return View(sitesModel);
}
Then in my view I have the following,
@model IEnumerable<trsDatabase.Models.masterViewModel>
foreach (var site in customer.CustomerSites)
{
foreach (var cust in customer.Customers)
{
<tr>
<td>
@cust.CustomerName
</td>
<td>
@site.UnitNo
</td>
using the above code I am able to access all properties from the two lists in the viewmodel, however when I navigate to the view I get an error as the view is expecting an IEnumerable. If I change the declaration to just pass the viewmodel
@model trsDatabase.Models.masterViewModel
the foreach
statement won't work, it gives this error
The model item passed into the dictionary is of type 'trsDatabase.Models.masterViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[trsDatabase.Models.masterViewModel]'.
Can anyone offer any advice or point me in the right direction for resolving this, is it possible to make my viewmodel IEnumerable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其更改
为此
您正在传递 masterViewModel 的单个实例,因此您的视图应该期望单个实例,这正是错误告诉您的内容(如果不是以神秘的方式)。
Change this
to this
You are passing in a single instance of masterViewModel, so your view should expect a single instance, which is exactly what the error is telling you if not in a cryptic way.
是的,你可以...
在您的模型 (masterViewModel) 中,像这样创建 Customers 和 CustomerSites 列表:
在同一个模型中,定义一个将返回 IEnumerable 的方法,如下所示:
在您的控制器中,实例化您的模型。然后调用 Getall() 方法,该方法将返回 IEnumerable 基本上是客户列表,并将其传递给您的视图
Yes You can...
in you Model (masterViewModel) make the Customers and CustomerSites List like this:
in the same Model, define a method that would return IEnumerable like this:
In your controller, instantiate your Model. then call Getall() method which would return IEnumerable basically a list of customers, and pass it to your View