2017-06-21 6 views
0

をMVC 4をアップロードバックファイルの後にメッセージでリダイレクトし、アクションビューを呼び出すために戻ってメッセージでリダイレクトしたい:と呼ばれるC#の私はMVC 4アプリケーションに取り組んできました

  1. 処置:アップロード
  2. 現在のビュー:インデックス

public class HospitalController: Controller { 
 
     public ActionResult Index() 
 
     { 
 
      return View(Model); 
 
     } 
 
     
 
     [HttpPost] 
 
     public ActionResult Index(Model model) 
 
     { 
 
      return View(ohosDetailFinal); 
 
     } 
 
     
 
     [HttpPost] 
 
     [ValidateAntiForgeryToken] 
 
     public ActionResult Upload(HttpPostedFileBase upload,FormCollection form) 
 
     { 
 
      //Here i want to pass messge after file upload and redirect to index view with message 
 
      // return View(); not working 
 
     } 
 
}
@using (Html.BeginForm("Upload", "Hospital", null, FormMethod.Post, new { enctype = "multipart/form-data", @class = "" })) 
 
{ 
 
    @Html.AntiForgeryToken() 
 
    @Html.ValidationSummary() 
 
    <input type="file" id="dataFile" name="upload" class="hidden" /> 
 
}

ありがとう!

+0

使用 ''リダイレクト、およびクエリ文字列値としてメッセージを渡すか、または 'Tempdata'に入れて取得するために(...)RedirectToActionを返しますそれはGETメソッドで –

答えて

1

PRGパターンに従います。処理が正常に終了したら、ユーザーを別のGETアクションにリダイレクトします。

RedirectToActionメソッドを使用してRedirectResultを返すことができます。これにより、ブラウザに304の応答が返され、新しいURLがロケーションヘッダーに追加され、ブラウザはそのURLに新しいGETリクエストを行います。

[HttpPost] 
public ActionResult Upload(HttpPostedFileBase upload,FormCollection form) 
{ 
    //to do : Upload 
    return RedirectToAction("Index","Hospital",new { msg="success"}); 
} 

今すぐインデックスアクションでは、この新しいパラメータmsgを追加し、この値をチェックし、適切なメッセージを表示することがあります。あなたがURLにクエリ文字列を好まない場合は、あなたがconsider using TempDataかもしれ

public ActionResult Index(string msg="") 
{ 
    //to do : check value of msg and show message to user 
    ViewBag.Msg = msg=="success"?"Uploaded successfully":""; 
    return View(); 
} 

とビューで

<p>@ViewBag.Msg</p> 

を:リダイレクト要求は、キーMSG(/Hospital/Index?msg=success例)でクエリ文字列を持つことになります。しかし、tempdataは次のリクエストに対してのみ利用可能です。

0
[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Upload(HttpPostedFileBase upload,FormCollection form) 
    { 
     //Here i want to pass messge after file upload and redirect to index view with message 
     return Index();//or with model index 
    } 
0

以下のコードを試してみてください。

ビュー

@using (Html.BeginForm("Upload", "Hospital", null, FormMethod.Post, new { enctype = "multipart/form-data", @class = "" })) 
     { 

     if (TempData["Info"] != null) 
     { 
     @Html.Raw(TempData["Info"]) 
     } 


     @Html.AntiForgeryToken() 
     @Html.ValidationSummary() 
     <input type="file" id="dataFile" name="upload" class="hidden" /> 
     } 

コントローラ

 [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Upload(HttpPostedFileBase upload,FormCollection form) 
     { 
     //Here i want to pass messge after file upload and redirect to index view with message 
     TempData["Info"]="File Uploaded Successfully!"; 
     return RedirectToAction("Index"); 
     } 
関連する問題