2014-02-06 4 views
0

양식을 제출 한 후 유효성 검사 오류가 발생하여 양식으로 다시 리디렉션하고 싶지만 URL이 페이지가 아닌 양식의 URL을 반영하는 상황 중 하나 동작.RedirectToAction() 손실 요청 데이터

viewData 매개 변수를 사용하면 POST 매개 변수가 GET 매개 변수로 변경됩니다.

어떻게 피 하시겠습니까? 해당 옵션이 GET 매개 변수로 변경되지 않았 으면합니다.

답변

1

유효성 검사 오류 bu의 경우 동일한 양식을 다시 렌더링하면 올바른 디자인 패턴이 리디렉션되지 않습니다. 작업이 성공한 경우에만 리디렉션해야합니다.

예 :

[HttpPost] 
public ActionResult Index(MyViewModel model) 
{ 
    if (!ModelState.IsValid) 
    { 
     // some validation error occurred => redisplay the same form so that the user 
     // can fix his errors 
     return View(model); 
    } 

    // at this stage we know that the model is valid => let's attempt to process it 
    string errorMessage; 
    if (!DoSomethingWithTheModel(out errorMessage)) 
    { 
     // some business transaction failed => redisplay the same view informing 
     // the user that something went wrong with the processing of his request 
     ModelState.AddModelError("", errorMessage); 
     return View(model); 
    } 

    // Success => redirect 
    return RedirectToAction("Success"); 
} 

이 패턴은 일부 오류가 발생하는 경우에 대비하여 모든 모델의 값을 보존 할 수 있으며 동일한보기를 다시 표시해야합니다.

+0

감사합니다. 이 주제가 유사한 문제를 발견했습니다. http://stackoverflow.com/questions/1936/how-to-redirecttoaction-in-asp-net-mvc-without-losing-request-data –