简体   繁体   English

将方法参数从模型传递到控制器

[英]pass method parameter from model to controller

public List<ProductlistModel> GetProductList(int catId)
{
   List<ProductlistModel> products = db.ProductLists.Where(c=>c.CategoryID==catId).Select(s => new ProductlistModel()
}

This is my product repository in Business Logic. 这是我在Business Logic中的产品存储库。 I am trying to pass catId in controller but it is throwing error. 我试图在控制器中传递catId ,但它抛出错误。

private ProductRepository pr = new ProductRepository();

public ActionResult Index()
{
    List<ProductlistModel> products = pr.GetProductList(catId);
    return View(products);
}

How to access catId in controller? 如何在控制器中访问catId

For example: 例如:

public ActionResult Index(int? catId)
{
    // TODO: check catId for null
    List<ProductlistModel> products = pr.GetProductList(catId.Value);
    return View(products);
}

Pass catId as parameter. 传递catId作为参数。 You can handle it by passing parameter as string and convert it to int before passing to the Function or Procedure of Model 您可以通过将参数作为字符串传递并将其转换为int,然后再传递给Model的Function或Procedure来处理它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM