将数据从存储库传递到视图模型时出现问题
我有以下视图模型,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SiteAdminCreateViewModel
类的构造函数定义如下:其第二个参数的类型为
Customer
。您将
var customer =repository.GetCustomers.ToList()
传递给它,其类型为List
。如果我理解你的意思是正确的,那么你只是试图传递客户列表来构建
SelectList
。首先,您似乎将一个字符串传递给
SelectList
构造函数。这甚至无法编译(请阅读 System.Web.Mvc.SelectList)。您需要做的是更改
SiteAdminCreateViewModel
的构造函数,例如CustomerId
和CustomerName
是Customer
的属性班级。Your
SiteAdminCreateViewModel
class' constructor is defined as follows:Its second argument is of type
Customer
.You're passing
var customer = repository.GetCustomers.ToList()
to it, whose type isList<Customer>
.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 likeCustomerId
andCustomerName
being properties of theCustomer
class.这只是因为您尝试将 IList 传递到 SiteAdminCreateViewModel 的构造函数中,
使用
会起作用,您还可能需要检查客户是否为空
it's simply because you are trying to pass IList into constructor of the SiteAdminCreateViewModel
using
would work, also you may need to check if the customer is null