2016-06-23 3 views
2

지금 내가하고있는 일은 TempData를 사용하여 내 ViewModel을 다른 Action으로 전달하는 것입니다.TempData를 사용하지 않고 ActionResults간에 ViewModel 전달

그러나 제 동료는 LoadBalancers 전에 TempData에 경험이있어서 TempData를 사용해서는 안된다는 사실을 알려 왔습니다.

여기 내 컨트롤러의 일부로 내가하고 싶은 것을 볼 수 있습니다. TempData 또는 Session을 사용하지 않고 어떻게 동일한 프로세스를 수행 할 수 있습니까? 감사합니다.

--EDIT--

public ActionResult Create() 
{ 
    MyViewModel viewModel; 

    if (TempData["viewModel"] != null) 
    { 
     viewModel = (MyViewModel)TempData["viewModel"]; 
     //code for getting dropdownlist to show to view 

     return View(viewModel); 
    } 

    viewModel = new RequestViewModel(); 
    //code for getting dropdownlist to show to view 

    return View(viewModel); 
} 

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(MyViewModel viewModel) 
{ 
    if (ModelState.IsValid) 
    { 
     //collect data, but not yet save to database 

     TempData["viewModel"] = viewModel; 

     return RedirectToAction("Confirm"); 
    } 

    //code to get errors, and dropdownlist items to re-show to view 
    return View(viewModel); 
} 

public ActionResult Confirm() 
{ 
    if (TempData["viewModel"] != null) 
    { 
     var viewModel = (MyViewModel)TempData["viewModel"]; 

     return View(viewModel); 
    } 

    return RedirectToAction("Create"); 
} 

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Confirm(MyViewModel viewModel) 
{ 
    if (ModelState.IsValid) 
    { 
     //save data to database if confirmed 

     return View(viewModel); 
    } 

    return RedirectToAction("Create"); 
} 

는 또한 매개 변수를 통해 redirectToAction하는 뷰 모델을 전달하는 시도했지만 내 뷰 모델은 리디렉션 후 다시 채워야하지 않았다. 코드 :

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(MyViewModel viewModel) 
{ 
    if (ModelState.IsValid) 
    { 
     //collect data, but not yet save to database 

     return RedirectToAction("Confirm", viewModel); 
    } 

    //code to get errors, and dropdownlist items to re-show to view 
    return View(viewModel); 
} 

public ActionResult Confirm(MyViewModel viewModel) 
{ 
    if (viewModel != null) 
    { 
     //some code 

     return View(viewModel); 
    } 

    return RedirectToAction("Create"); 
} 

[HttpPost] 
[ActionName("Review")] 
[ValidateAntiForgeryToken] 
public ActionResult ConfirmPost(MyViewModel viewModel) 
{ 
    if (ModelState.IsValid) 
    { 
     //save data to database if confirmed 

     return View(viewModel); 
    } 

    return RedirectToAction("Create"); 
} 

답변

2

페이지로드에 대한 검사 방법은 당신이 당신의 상황에서

return View("Check", viewmodel); 
+0

아니가,보기를 확인이다. 사용자가 취소 버튼을 눌러 취소하거나 계속 진행합니다. – jomsk1e

+0

오해 때문에 죄송합니다. 새보기를 확인 하시겠습니까? –

+0

예, 확인은 사용자가 입력 내용의 유효성을 검사 한 다음 계속할 것인지 결정할 수있는 새로운보기입니다. – jomsk1e

0

문제를 사용하지 수, 아무것도 할 수 있지만보기를 반환하지 않는 가정은 사실 때문입니다 TempData 다중 서버 환경에서 문제가되는 내용을 저장하기 위해 서버 세션을 사용합니다. 쿠키를 대신 사용하려면 자체 tempdata 공급자를 구현할 수 있습니다. 몇 가지 가능한 솔루션 : 사용자 확인 모든 입력이 올바른지 곳

https://stackoverflow.com/a/28355862/1942895

https://brockallen.com/2012/06/11/cookie-based-tempdata-provider

http://vijayt.com/Post/Custom-TempDataProvider-for-Azure

관련 문제