views:

45

answers:

2

Hi, this is the case: ASP.NET MVC C#

Model: I would like to use information defined on three tables, Product - Details - Pictures. I created a ProductRepository and defined a join of the three tables. Finally I select fields from each of these tables.

    public IQueryable ProductsList()
    {
        var db = new Entities();
        var results = from p in db.Products
                    join pi in db.ProductPic on p.ProductId equals pi.ProductId
                    // first join
                    join pv in db.ProductDetails on p.ProductId equals pv.ProductId
                    // second join
                    select new
                               {
                                   p.Name,
                                   p.Description,
                                   p.PCategory,
                                   p.ProductPicture,
                                   pv.Price,
                                   pi.Picture,
                               };
        return (results);
    }

Controller: I defined:

    public ActionResult Index()
    {
        var products = productrepository.ProductsList();
        if (products == null) throw new NotImplementedException();
        else
        return View("Index", products);
    }

Views: I created a strongly typed view from model Products.

When I create the view I only received the fields from Table Product, but not the fields from table Details and Picture.

My main question resides in the way it has to be created a View in order to use all the fields defined on the model (in this case = productrepository.productslist(); At this moment when I scaffold directly I only receive fields from product using strongly typed with product.

Thank you so much!.

+1  A: 

You may want to return a named type from ProductsList() instead of an anonymous one. You could then use that type name in your view.

Matthew Manela
hi thank you, I´m confused because I made joins into the model, I passed to the controller and finally when I tried to use those fields in my view, I can only choose - using strongly typed options - just unique tables but not what I created using fields of three of them...
sebastian_h
ok, I understood. Thank you.
sebastian_h
+2  A: 

You strongly-typed your view to a Product type but your EF query returns an anonymous type that includes other fields. Create a class for the product view model:

class ProductViewModel
{
    public string Name { get; set; }
    public string Description{ get; set; }
    public string PCategory{ get; set; }
    public string ProductPicture{ get; set; }
    public string Price { get; set; }
    public string Picture { get; set; }
}

Then in the select of your EF query, populate this type rather than your anonymous type. Also, you should independently verify that your EF query is returning the expected results.

Steve Michelotti
ahh, ok... I understood. Thank you so much..
sebastian_h
Steve, I´m having problems. I tried to create a ProductViewModel class in order to display the fields I want to but I cannot create a result.First I have problems defining public string, it is not accepted in many fields. Additionally I place the return information using the return View("Index", class-name); Any help appreciated...
sebastian_h
continue: but with index, class-name didnt work. Additionally, there is no place where I can define the return of the productlist... Thank you so much in advance if your read this post.
sebastian_h
@sebastian - I'm not following - what do you mean when you say "it is not accepted in many fields"? I'm also not sure what you mean when you say you can't "define the return of the productlist". It would help to be able to see code and specific error messages.
Steve Michelotti
Steve, sorry for my delay. I paste the whole code in http://www.copypastecode.com/45542/
sebastian_h
I dont know how to use the ProductViewModel that you suggested me to use. possibility 1: place it inside the select{Productviewmodel} of the repository. possibility 2: place it in the return(productviewmodel) of the controller?. Honestly, I dont know how to pass the info of both tables, using the repository class and invoking only the fields I need to display... Brgds! thank you.
sebastian_h
The simplest approach is to change your Select so that instead of an anonymous type, it is: select new ProductViewModel { Name = p.Name, Description = p.Description, (rest of the properties here)....... }; Given this is just displaying a read-only list, that approach is generally acceptable.
Steve Michelotti
Thank you so much!, I will do that.
sebastian_h