MVC3: PRG Pattern with Search Filters on Action Method

前端 未结 2 2059
梦如初夏
梦如初夏 2020-12-06 23:39

I have a controller with an Index method that has several optional parameters for filtering results that are returned to the view.

public ActionResult Inde         


        
2条回答
  •  -上瘾入骨i
    2020-12-07 00:13

    A redirect is just an ActionResult that is another action. So if you had an action called SearchResults you would simply say

    return RedirectToAction("SearchResults");
    

    If the action is in another controller...

    return RedirectToAction("SearchResults", "ControllerName");
    

    With parameter...

    return RedirectToAction("SearchResults", "ControllerName", new { parameterName = model.PropertyName });
    

    Update

    It occurred to me that you might also want the option to send a complex object to the next action, in which case you have limited options, TempData is the preferred method

    Using your method

    [HttpPost]
    public ActionResult Index(ViewModel model) {
        ...
        if (ModelState.IsValid) {
            product = repository.GetProducts(model);
            TempData["Product"] = product;
            return RedirectToAction("NextAction");
        }
        return View(model);
    }
    
    public ActionResult NextAction() {
        var model = new Product();
        if(TempData["Product"] != null)
           model = (Product)TempData["Product"];
        Return View(model);
    }
    

提交回复
热议问题