views:

52

answers:

3
public ActionResult Index()
{ 
    var pr = db.products;
    return View(pr); 
}

Firstly - I want to pass to the view more data - something like:

public ActionResult Index()
{ 
    var pr = db.products;
    var lr = db.linksforproducts(2)
    return View(pr,lr); 
}

How do I read the lr data in the view?

Secondly - on the view I have a table of products, and I want to add to the table a column with all the tags of this products. How do I get the tags for every product?

now i create this code

public class catnewModel
{

    public IQueryable<category> dl { get; set;   }
    public IQueryable<product> dr { get; set;   }
}

and my controller

public ActionResult Index()
    {

        var pr = db.products;
        var pl = db.categories;

        catnewModel model = new catnewModel();
        model.dr = pr;
        model.dl = pl;

        return View(model);
    }

in my view i try to iterate over

 <% foreach (var item in Model.dr)  %>

but i get error on

error CS1061: 'System.Collections.Generic.IEnumerable<amief.Models.catnewModel>' does not contain a definition for 'dr' and no extension method 
+3  A: 

I have done this by making a ViewModel specific to the view you need the information in.

Then within that ViewModel just have properties to house your other models.

Something like this:

public class ViewModel
{
    public List<ProductModel> Products(){get; set;}
    public List<LinksForProductModel> LinksForProducts(){get; set;}
}


public ActionResult Index()
{ 
    var pr = db.products;
    var lr = db.linksforproducts(2)

    ViewModel model = new ViewModel();
    model.Products = pr;
    model.LinksForProducts = lr;


    return View(model); 
}
jimplode
where is the bast place to put the ViewModel class?i need to put it in the models folder?
eyalb
I seperate the model from viewmodels. Your models should represents the things in the system, people, cars, products etc as well as the business logic and validation logic. Your viewmodel is particular model specifically for that view, so I stick it in a folder called ViewModels.
jimplode
in the view i try to get to model.Products but i get an error.
eyalb
Is your view of the correct strongly typed class??
jimplode
i think, that the class is ok.
eyalb
What error are you getting???
jimplode
+1  A: 

Usually - You create view model per view.

In Your case, that would be:

public class IndexModel{
  public ProductModel[] Products{get;set;}
  public LinkForProduct[] Links{get;set;}
}

public ActionResult Index(){
  var model=new IndexModel{
    Products=Map(db.products), 
    Links=Map(db.linksforproducts(2)};
  return View(model);
}
Arnis L.
+1  A: 

Create a view model containing two properties:

public class MyViewModel
{
    public IEnumerable<Product> Products { get; set; }
    public IEnumerable<LinkProduct> Links { get; set; }
}

And in your controller:

public ActionResult Index()
{ 
    var model = new MyViewModel
    {
        Products = db.products,
        Links = db.linksforproducts(2)
    };
    return View(model); 
}
Darin Dimitrov