问题描述:

In MVC view i want to create search to get product by id , But not one search i want more than one search that cause the customer want more than product

`

public ActionResult Search(int pid ,int pid2 ,int pid3)

{

var products = (from d in db.Products

where d.ProductId == pid || d.ProductId == pid2 || d.ProductId == pid2

select d).ToList();

return View(products);

}`

view show what i mean

When i try to do this with this code ,all search results be the same result

网友答案:

This is arm chair coded so it might take a little finessing to actually work but you want something like:

public ActionResult Search(IEnumerable<int> productIds)
{
    var products = (from d in db.Products 
          where productIds.Contains(d.ProductId);
          select d).ToList();

            return View(products);
}

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ is also likely relevant here.

A different topic is you should not be doing database calls inside a controller. You should move your database logic to a separate class and then use constructor injection to pass that class instance to your controller.

相关阅读:
Top