2015-01-05 3 views
2

한 컨트롤러에서 다른 컨트롤러로 모델 데이터를 전달할 수 있습니까?한 컨트롤러에서 다른 컨트롤러로 모델 데이터를 전달하는 방법

다른 컨트롤러의 한 컨트롤러에 모델 데이터를 보내고 싶습니다. nextStep 컨트롤러 모델 값

[HttpPost] 
     public ActionResult Personal(StudentModel student) 
     {       
       return RedirectToAction("nextStep", new { model = student});   
     } 

     public ActionResult nextStep(StudentModel model) 
     {   
      return View(model); 
     } 

널이다. 어떻게 수행하나요? nextStep conreoller에서 StudentModel 데이터가 필요합니다.

+0

[가능한 다른 컨트롤러 작업 방법간에 데이터 전달] (http://stackoverflow.com/questions/15385442/passing-data-between-different-controller-action-methods) –

답변

6

RedirectToAction을 사용 중입니다. GET 요청을 발행합니다. 여기에 모델을 전달할 수있는 두 가지 방법이 있습니다.

1 TempData

당신은 TempData에서 모델을 지속하고 RedirectToAction을 확인해야합니다. 그러나 제한 사항은 즉각적인 요청에 대해서만 사용할 수 있다는 것입니다. 귀하의 경우에는 문제가되지 않습니다. 당신은 요청을 우리는 모델 속성 이름과 쿼리 문자열로 데이터를 전달할 수 있습니다, GET이기 때문에

쿼리 문자열에 전달

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

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

2 TempData

와 함께 할 수 있습니다. MVC 모델 바인더는 쿼리 문자열을 분석하여 모델로 변환합니다.

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

은 또한 쿼리 문자열에 합리적인 데이터를 전달 메모 을하는 것은 권장하지 않습니다.

관련 문제