Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying results of a Stored Procedure in MVC View (EF 5 / MVC 4)

The goal

I want to display in my view the results of stored procedure.

The problem

Entity Framework automatically imported for me a method that executes a procedure, however I'm not getting the results I expect displaying on the screen.

The imported function is:

public virtual ObjectResult<getProductsListForHome_Result> getProductsListForHome(Nullable<int> inOffer, Nullable<int> categoryId)
{
    var inOfferParameter = inOffer.HasValue ?
        new ObjectParameter("inOffer", inOffer) :
        new ObjectParameter("inOffer", typeof(int));

    var categoryIdParameter = categoryId.HasValue ?
        new ObjectParameter("categoryId", categoryId) :
        new ObjectParameter("categoryId", typeof(int));

    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<getProductsListForHome_Result>("getProductsListForHome", inOfferParameter, categoryIdParameter);
}

What I have already tried

On ProductsController:

//
// GET: /Products/
public ActionResult Index()
{
    ObjectResult<getProductsListForHome_Result> products = db.getProductsListForHome(1, 14);
    return View(products.ToList());
}

Using the previous code, when I access http://myapp.com/Products/ I'm getting the following message:

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[MyApp.Models.getProductsListForHome_Result]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MyApp.Models.bm_products]'.

What do I have to do to resolve this?

like image 790
Guilherme Oderdenge Avatar asked Jun 12 '13 14:06

Guilherme Oderdenge


1 Answers

First, well-written question!

This is a type-casting problem, and it looks like your answer is the accepted answer here:

MVC: The model item passed into the dictionary is of type X, but this dictionary requires a model item of type X

like image 90
Dave Swersky Avatar answered Sep 25 '22 15:09

Dave Swersky