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 custome开发者_如何学编程rs 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.
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.
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
精彩评论