.NET MVC : Calling RedirectToAction passing a model?

后端 未结 4 1859
耶瑟儿~
耶瑟儿~ 2020-12-03 04:20

I got a view List.aspx that is bound to the class Kindergarten

In the controller:

public ActionResult List(int Id)
{
  Kind         


        
4条回答
  •  暖寄归人
    2020-12-03 05:09

    I don't believe ModelBinding exists when using RedirectToAction. Your best options, however, is to use the TempData collection to store the object, and retrieve it in the following action.

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Add(...)
    {
      //...
      Kindergarten k = ...
      TempData["KG"] = k;
      return RedirectToAction("List");
    }
    

    In your List Action

    public ActionResult List()
    {
    
       Kindergarten k = (Kindergarten)TempData["KG"];
       // I assume you need to do some stuff here with the object, 
       // otherwise this action would be a waste as you can do this in the Add Action
      return View(k);
    }
    

    Note: TempData collection only holds object for a single subsequent redirect. Once you make any redirect from Add, TempData["KG"] will be null (unless you repopulate it)

提交回复
热议问题