RedirectToAction
errorMsg 値のクエリ文字列を使用して、ページにリダイレクトするために使用できます。
[HttpPost]
public ActionResult Index(YourViewModel model)
{
try
{
//try to save and then redirect (PRG pattern)
}
catch(Exception ex)
{
//Make sure you log the error message for future analysis
return RedirectToAction("Index",new { errorMs="something"}
}
}
RedirectToAction
リクエストを発行しGET
ます。HTTP は statelessであるため、フォームの値はなくなります。フォームの値をそのままフォームに残したい場合は、投稿されたビューモデル オブジェクトを再度返します。ViewBag を取り除きErrorMsg
、ViewModel に呼び出される新しいプロパティを追加して、その値を設定します。
[HttpPost]
public ActionResult Index(YourViewModel model)
{
try
{
//try to save and then redirect (PRG pattern)
}
catch(Exception ex)
{
//Make sure you log the error message for future analysis
model.ErrorMsg="some error";
return View(model);
}
}
ビューでは、このモデルのプロパティを確認して、ユーザーにメッセージを表示できます。