如何制作 IEnumerable 类型的 Viewmodel?

发布于 2024-11-07 00:23:20 字数 1297 浏览 0 评论 0原文

我将以下代码用于包含两个数据列表的主视图模型,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下的泡沫是彩色的 2024-11-14 00:23:20

将其更改

@model IEnumerable<trsDatabase.Models.masterViewModel>

为此

@model trsDatabase.Models.masterViewModel

您正在传递 masterViewModel 的单个实例,因此您的视图应该期望单个实例,这正是错误告诉您的内容(如果不是以神秘的方式)。

Change this

@model IEnumerable<trsDatabase.Models.masterViewModel>

to this

@model trsDatabase.Models.masterViewModel

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.

追风人 2024-11-14 00:23:20

是的,你可以...
在您的模型 (masterViewModel) 中,像这样创建 Customers 和 CustomerSites 列表:

namespace trsDatabase.Models
{
public class masterViewModel
{
    public List<Customer> Customers { get; set; }
    public List<CustomerSite> CustomerSites { get; set; }
}
}

在同一个模型中,定义一个将返回 IEnumerable 的方法,如下所示:

public IEnumerable<Customer> Getall()
        {
            List<Customer> lcustomer= new List<Customer>();
            //Get Customer data from Database or wherever
            lcustomer.Add(new Customer{ firstname= "Quentin ", lastname= "tarantino" });
            return lcustomer;
        }

在您的控制器中,实例化您的模型。然后调用 Getall() 方法,该方法将返回 IEnumerable 基本上是客户列表,并将其传递给您的视图

var rep = new masterViewModel();
var model = rep.Getall();
return View(model);

Yes You can...
in you Model (masterViewModel) make the Customers and CustomerSites List like this:

namespace trsDatabase.Models
{
public class masterViewModel
{
    public List<Customer> Customers { get; set; }
    public List<CustomerSite> CustomerSites { get; set; }
}
}

in the same Model, define a method that would return IEnumerable like this:

public IEnumerable<Customer> Getall()
        {
            List<Customer> lcustomer= new List<Customer>();
            //Get Customer data from Database or wherever
            lcustomer.Add(new Customer{ firstname= "Quentin ", lastname= "tarantino" });
            return lcustomer;
        }

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

var rep = new masterViewModel();
var model = rep.Getall();
return View(model);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文