How to pass model data to one controller to another controller

后端 未结 1 584
挽巷
挽巷 2021-02-04 21:35

pass model data to one controller to another controller is possible?

I want to passe the model data to one controller ro another controller.

[HttpPost]
          


        
相关标签:
1条回答
  • 2021-02-04 22:08

    You are using RedirectToAction. It will issue GET request. There are two ways you can pass your model here.

    1. TempData

    You need to persist the model in TempData and make RedirectToAction. However the restriction is it will be available only for the immediate request. In your case, it is not a problem. You can do it with TempData

    public ActionResult Personal(StudentModel student)
    {                          
           TempData["student"] = student;
           return RedirectToAction("nextStep", "ControllerName");          
    }
    
    public ActionResult nextStep()
    {      
           StudentModel model= (StudentModel) TempData["student"];
           return View(model);
    }
    

    2. Passing in a query string

    As the request is GET, we can pass the data as Query string with the model property name. MVC model binder will resolve the query string and convert it as model.

     return RedirectToAction("nextStep", new { Name = model.Name, Age=model.Age });
    

    Also take a note passing sensible data in a Query string is not advisable.

    0 讨论(0)
提交回复
热议问题