我正在研究MVC 4应用程序,并希望通过消息重定向到调用操作视图:
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 :(得分: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>
如果您不喜欢网址中的查询字符串,则可以consider using TempData。但tempdata仅适用于下一个请求。
答案 1 :(得分:0)
DateTimeTypeConverter
答案 2 :(得分: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");
}