将数据从存储库传递到视图模型时出现问题

发布于 2024-11-27 23:07:46 字数 1398 浏览 1 评论 0原文

我有以下视图模型,

public class SiteAdminCreateViewModel
{
    public Customer Customer { get; private set; }
    public CustomerSite CustomerSite { get; private set; }
    public SelectList CustomerNames { get; private set; }

    public SiteAdminCreateViewModel(CustomerSite customerSite, Customer customer)
    {
        CustomerSite = customerSite;
        Customer = customer;
        CustomerNames = new SelectList(customer.CustomerName);
    }
}

以及我的存储库中的以下方法,用于返回客户列表和 CustomerSites 列表

 public IQueryable<CustomerSite> GetCustomerSites
    {
        get { return context.CustomerSites; }
    }
    public IQueryable<Customer> GetCustomers
    {
        get { return context.Customers; }
    }

当我在控制器中实例化视图模型时,我想返回客户列表以传递到视图模型中的选择列表。

 public ViewResult Create() 
    {
        CustomerSite customerSite = new CustomerSite();
        var customer = repository.GetCustomers.ToList();
        return View(new SiteAdminCreateViewModel(customerSite, customer));       
    }

但返回行抛出错误

无法从 System.Collections.Generic.List' 转换为 'CustomerOrders.Domain.Entities.Customer

我认为这是因为我在 Customer 类型的 Viewmodel 中定义了 customer 变量,但我试图传递客户名单?

谁能就我在这里出错的地方提供任何建议?

我是否需要在视图模型中定义客户类型和客户名称选择列表类型,我仅定义了客户对象,以便我可以使用它将客户传递到选择列表,但我不确定这是否是最好的方法这?

任何人可以为新手提供的任何建议将不胜感激。

I have the following viewmodel,

public class SiteAdminCreateViewModel
{
    public Customer Customer { get; private set; }
    public CustomerSite CustomerSite { get; private set; }
    public SelectList CustomerNames { get; private set; }

    public SiteAdminCreateViewModel(CustomerSite customerSite, Customer customer)
    {
        CustomerSite = customerSite;
        Customer = customer;
        CustomerNames = new SelectList(customer.CustomerName);
    }
}

And the following methods in my repository for returning a list of customers and a list of CustomerSites

 public IQueryable<CustomerSite> GetCustomerSites
    {
        get { return context.CustomerSites; }
    }
    public IQueryable<Customer> GetCustomers
    {
        get { return context.Customers; }
    }

When i instanitiate the viewmodel in my controller im wanting to return the list of customers to passs to the select list in the viewmodel.

 public ViewResult Create() 
    {
        CustomerSite customerSite = new CustomerSite();
        var customer = repository.GetCustomers.ToList();
        return View(new SiteAdminCreateViewModel(customerSite, customer));       
    }

But the return line throws the error

cannot convert from System.Collections.Generic.List' to 'CustomerOrders.Domain.Entities.Customer

I think this is because i have the customer variable defined in the Viewmodel of type Customer but im trying to pass a list of customers?

Can anyone offer any advice on where i am going wrong here?

Do i need to define both the Customer type and the CustomerNames select list type in the viewmodel, i defined the Customer Object only so i can use it to pass the Customers to the select list but im not sure if this is the best way to do this?

Any advice anyone can offer for a newbie, will be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

情独悲 2024-12-04 23:07:46

SiteAdminCreateViewModel 类的构造函数定义如下:

public SiteAdminCreateViewModel(CustomerSite customerSite, Customer customer) {
   ...
}

其第二个参数的类型为 Customer

您将 var customer =repository.GetCustomers.ToList() 传递给它,其类型为 List


当我在控制器中实例化视图模型时,我想要
返回要传递到的选择列表中的客户列表
视图模型。

如果我理解你的意思是正确的,那么你只是试图传递客户列表来构建 SelectList

首先,您似乎将一个字符串传递给 SelectList 构造函数。这甚至无法编译(请阅读 System.Web.Mvc.SelectList)。

您需要做的是更改 SiteAdminCreateViewModel 的构造函数,例如

public SiteAdminCreateViewModel(CustomerSite customerSite, IEnumerable<Customer> customers) {
   /* ... */
   CustomerNames = new SelectList(customers, "CustomerId", "CustomerName");
}

CustomerIdCustomerNameCustomer 的属性班级。

Your SiteAdminCreateViewModel class' constructor is defined as follows:

public SiteAdminCreateViewModel(CustomerSite customerSite, Customer customer) {
   ...
}

Its second argument is of type Customer.

You're passing var customer = repository.GetCustomers.ToList() to it, whose type is List<Customer>.


When i instanitiate the viewmodel in my controller im wanting to
return the list of customers to passs to the select list in the
viewmodel.

If I understand what you're saying correctly, you're just trying to pass the customers list to build a SelectList.

First of all, you seem to be passing a string to the SelectList constructor. This would not even compile (read System.Web.Mvc.SelectList).

What you'd need to do is change SiteAdminCreateViewModel's constructor like

public SiteAdminCreateViewModel(CustomerSite customerSite, IEnumerable<Customer> customers) {
   /* ... */
   CustomerNames = new SelectList(customers, "CustomerId", "CustomerName");
}

CustomerId and CustomerName being properties of the Customer class.

耀眼的星火 2024-12-04 23:07:46

这只是因为您尝试将 IList 传递到 SiteAdminCreateViewModel 的构造函数中,

var customer = repository.GetCustomers.ToList();
        return View(new SiteAdminCreateViewModel(customerSite, customer));

使用

var customer = repository.GetCustomers.ToList().FirstOrDefault();
            return View(new SiteAdminCreateViewModel(customerSite, customer));

会起作用,您还可能需要检查客户是否为空

it's simply because you are trying to pass IList into constructor of the SiteAdminCreateViewModel

var customer = repository.GetCustomers.ToList();
        return View(new SiteAdminCreateViewModel(customerSite, customer));

using

var customer = repository.GetCustomers.ToList().FirstOrDefault();
            return View(new SiteAdminCreateViewModel(customerSite, customer));

would work, also you may need to check if the customer is null

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