【问题标题】:Redirect back with message after file upload MVC 4 c#文件上传 MVC 4 c# 后用消息重定向回来
【发布时间】:2017-06-21 11:01:35
【问题描述】:

我正在开发 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" />
}

谢谢!

【问题讨论】:

  • 使用return RedirectToAction(...)进行重定向,并将消息作为查询字符串值传递或放入Tempdata并在GET方法中检索

标签: c# asp.net-mvc


【解决方案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 并检查它的值并显示适当的消息。重定向请求将有一个带有键 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 仅适用于下一个请求。

【讨论】:

    【解决方案2】:
        [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
        }
    

    【讨论】:

      【解决方案3】:

      试试下面的代码,希望对你有帮助!

      查看

          @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");
            }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-15
        • 2012-12-12
        • 2023-03-03
        • 2019-07-18
        • 2018-02-16
        • 1970-01-01
        • 1970-01-01
        • 2016-09-19
        相关资源
        最近更新 更多