Pergunta

Hi This is my Action method

public ActionResult Welcome(string name, int numTimes=1)
{
    var customerEntityColl = new EntityCollection(new CustomerEntityFactory());
    var customerEntity = new CustomerEntity(1);
    using(var adapter = new DataAccessAdapter())
    {            
        adapter.FetchEntityCollection(customerEntityColl,null);
    }
    return View(customerEntityColl);            
}

and this is my View

@model Data.HelperClasses.EntityCollection<Data.EntityClasses.CustomerEntity>

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@{

}

<p>This is our welcome view and the sender name is @ViewBag.Name and sent @ViewBag.NumTimes times this request</p>

@foreach (var item in Model)
{
    <p>@Html.DisplayFor(modelItem=> item.FirstName)</p>
}

Now when I run this action method it gives me below error, can somebody please help to fix it..

The model item passed into the dictionary is of type 'Data.HelperClasses.EntityCollection', but this dictionary requires a model item of type 'Data.HelperClasses.EntityCollection`1[Data.EntityClasses.CustomerEntity]'.

Thanks

Foi útil?

Solução

Yes, you're instantiating EntityCollection, and not EntityCollection<CustomerEntity>() which is what the model expects.

You can just replace new EntityCollection(new CustomerEntityFactory()) with new EntityCollection<CustomerEntity>(); (assuming you didn't change any behavior in the CustomerEntityFactory)

Additionally, you should fetch the entity with a Predicate, this is not doing anything at the moment when you're using the adapter model:

var customerEntity = new CustomerEntity(1);

and this will just fetch all the customers in the database:

adapter.FetchEntityCollection(customerEntityColl,null);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top