0

I know this is something simple but I can't figure it out :(

I'm receiving this error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[<>f__AnonymousType35[System.String,System.String,System.String,System.Nullable1[System.DateTime],System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MvcApplication3.Order]'.

Here is the code for my controller:

    var query = from o in db.Orders
                join c in db.Customers on o.CustomerID equals c.CustomerID
                where o.CustomerID == q
                select new
                {
                    o.CustomerID,
                    o.ShipAddress,
                    o.ShipCity,
                    o.ShippedDate,
                    c.ContactName
                };

    /*
    var query = from o in db.Orders
                where o.CustomerID == q
                select o;
    */
    return View(query.ToList());

Here is my view:

@model IEnumerable<MvcApplication3.Order>


@{
    ViewBag.Title = "Search";
}

<h2>Search</h2>
Your search results contains @Model.Count() rows

<table>
    <tr>
        <td>Customer ID</td>
        <td>Address</td>
        <td>City</td>
        <td>Shipped to Date</td>
    </tr>
@foreach (var item in Model)
{
    <tr>
        <td>@item.CustomerID</td>
        <td>@item.ShipAddress</td>
        <td>@item.ShipCity</td>
        <td>@item.ShippedDate</td>
        <td>@item.Customer.ContactName</td>

    </tr>
}
</table>

I'm assuming it has something to do with the way View is setup. If I remove the return View() and just loop through the data from the controller I get the results I expect. Any help would be greatly appreciated.

Thanks in advance!

2 Answers 2

1

EDIT:

Try this:

select new Order()
                    {
                        CustomerId = o.CustomerID,
                        ShipAddress = o.ShipAddress,
                        ShipCity = o.ShipCity,
                        ShippedDate = o.ShippedDate,
                        Customer = new Customer()
                              {
                                 ContactName = c.ContactName
                              }
                    };

You didn't specify the model. You created a List of anonymous objects, and the view is expecting an IEnumerable<Order>.

Sign up to request clarification or add additional context in comments.

4 Comments

if I put that in, I receive "Error 2 Cannot initialize type 'MvcApplication3.Order' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
sorry, i didn't pay enough attention. Check my edit. Also replace new Customer with the model name for your Customer variable in the Order model.
Thank you. It's working. Would a ViewModel class file also help with this scenario?
It would help, but i prefer the MVCR pattern. Basically, you create another class called Repository, that manages the database. There i define methods like GetCustomers(), GetOrders(), DeleteCustomer(), etc. In the controller, i initialize the model, call the repository methods to fill the model with data and send it to view.
0

You can also try just like this.

var query = (from o in db.Orders
                join c in db.Customers on o.CustomerID equals c.CustomerID
                where o.CustomerID == q
                select c);

and in view you can change it to

@model IEnumerable < MvcApplication3.Customers >

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.