Cannot iterate through collection
OK... I've been beating my head against the wall on this one for hours. What seems like a simple thing to fix, I can't come up with a proper solution. This is an MVC 3 website.
I have a web service which successfully returns an Entity Framework Customer object in my Controller with one record. It can contain many Customer objects, but in this case,开发者_如何转开发 it just has one record.
YeagerTechWcfService.Customer custs = db.GetCustomers();
return View("Index", custs);
When trying to iterate through the Customer object, I'm getting the following error msg:
Cannot cast '((System.Web.Mvc.WebViewPage)(this)).ViewData.Model' (which has an actual type of 'YeagerTech.YeagerTechWcfService.Customer') to 'System.Collections.Generic.IEnumerable'
It obvioulsy can't cast my Customer object to an IEnumberable object and I don't know why.... Can't you cast the EF objects to an IEnumerable object when trying to go through each "Customer" record?
In my View, I have the following code. The ViewData.Model has the data of the Customer record in it. @model YeagerTech.YeagerTechWcfService.Customer
@if (ViewData.Model != null)
{
foreach (YeagerTech.YeagerTechWcfService.Customer item in (IEnumerable<YeagerTech.YeagerTechWcfService.Customer>)ViewData.Model)
Can someone please let me know how I can resolve this?
You have a mix-up of list (or enumerable) of customers and a single customer. It start with the following line:
YeagerTechWcfService.Customer custs = db.GetCustomers();
The declaration on the left side clearly says it's a single customer, yet the method name on the right side indicates that you're somehow expecting a list of customers.
In the view, you declare the model to be a single customer:
@model YeagerTech.YeagerTechWcfService.Customer
But then you don't use the strongly typed instance variable Model
but instead use the untyped property ViewData.Model
and cast it to something it isn't (thus getting the error).
As it stands now, db.GetCustomers()
returns a single customer (not a list with a single customer). So remove the foreach loop from the view and just write:
@if (Model != null)
{
<p>@Model.FirstName @Model.LastName</p>
...
}
The other approach is to fix db.GetCustomers()
so it indeed returns a list of customers. Then you have to change your controller to:
List<YeagerTechWcfService.Customer> customerList = db.GetCustomers();
return View("Index", customerList );
and your view:
@model List<YeagerTech.YeagerTechWcfService.Customer>
@if (Model != null)
{
foreach (YeagerTech.YeagerTechWcfService.Customer item in Model)
{
<p>@item.FirstName @item.LastName</p>
....
}
}
There shouldn't be any need to use ViewData.Model
or type cast. They just cover type mismatches.
The problem has nothing to do with EF. You simply can't cast Customer
object to IEnumerable<Customer>
.
I'm not sure why you want to iterate in the view when you pass a Customer
object from the controller as the Model.
If your GetCustomers()
method's return type is YeagerTech.YeagerTechWcfService.Customer
then you can create a list and pass it to view.
var customers = new List<YeagerTech.YeagerTechWcfService.Customer>(db.GetCustomers());
return View("Index", customers);
I suggest you review your design.
i cnt say for sure since i cant see your code but i am assuming that your view is set as a strongly typed Customer view if you change it to IEmumurable it should fix the problem
edit: change @model line from Customer
to IEnumerable<Customer>
and this should fix your error judging from the error message you have it set as a strongly typed view of type Customer which is only going to allow you to use a singular customer to use an IEnumerable
the view model line need to be redefined to mtch this or you will recieve this error forever if this does not fix your error please update your question with the full source of your view so we can see everything
Try this...
YeagerTechWcfService.Customer custs = db.GetCustomers().ToList();
Update Here is code I have in existing website that returns an enumerable list and I iterate over the list in my controller.
public List<Subscriber> GetSubscribers()
{
using (var database = new Data.MyCoolDatabase())
{
return database.Subscribers.ToList();
}
}
In your view make sure your model statement is an IEnumerable like so...
@model IEnumerable<Newsletter.Web.Models.Subscriber>
精彩评论