2017-06-21 41 views
0

我對MVC 4應用程序的工作,並希望重定向回來的消息調用操作觀點:重定向回來了文件後,郵件上傳MVC 4 C#

  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

使用'返回RedirectToAction(...)'重定向,並通過消息作爲查詢字符串值,或把它放在'Tempdata'和檢索它在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,並檢查該值,並顯示相應的信息。重定向請求,將與鍵味精查詢字符串(例如:/Hospital/Index?msg=success

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> 

如果你不喜歡的查詢字符串的URL,你可能consider using TempData。但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"); 
     }