【问题标题】:View in ActionResult not working in ASP.NET MVC 5在 ActionResult 中查看在 ASP.NET MVC 5 中不起作用
【发布时间】:2015-04-15 03:56:04
【问题描述】:

在我的 ASP.NET MVC 5 项目中,我有一个 ActionResult SaveUploadedFile 和一个 ActionResult DashboardSaveUploadedFile 重定向到 Action Dashboard

public ActionResult SaveUploadedFile()
{
     //Call to WEB API
     ...
     ...
     //On Success of API
     if (results.payload.success == true)
     {
         return RedirectToAction("Dashboard");
     }

 throw new HttpException(403, "Something happened");

} 

public ActionResult Dashboard()
{
     return View();
} 

ActionResult XXX 是从我的 HTML 表单中调用的。

 <form action="~/Home/SaveUploadedFile" method="post" enctype="multipart/form-data" class="dropzone" id="dropzoneForm"></form>

控件到达ActionResult Dashboard,但对应的Dashboard View 未呈现。当从其他 ActionResults 调用时,Dashboard 可以完美运行。

我错过了什么吗?

【问题讨论】:

  • 你能发布实际代码吗?你所描述的应该在理论上有效。 t 必须是别的东西。例如,YYY 返回的视图中究竟包含什么。你看过 Fiddler 的线索吗?
  • 另外,您使用的是什么浏览器。已经报告了 MVC 5 中 RedirecttoAction 的 IE 问题 - stackoverflow.com/questions/22096474/…
  • 我正在使用 Chrome 39.0.2171.71
  • 好的。但根据此代码,仪表板返回一个空视图。仪表板只是一个没有控制器信息代码的 HTML 页面吗?我不确定您如何确定它没有触发。另外,如果 results.payload.success 为 false 并且没有抛出异常会怎样?
  • 您的代码看起来不太好。您确定您的条件(results.payload.success)通过了吗?渲染了什么?一个空白页,一个错误?

标签: asp.net forms post razor asp.net-mvc-5


【解决方案1】:

这应该会暴露问题所在:

    public ActionResult SaveUploadedFile()
    {
         //Call to WEB API
         ...
         ...
         //On Success of API
         if (results.payload.success == true)
         {
             return RedirectToAction("Dashboard");
         } else {
        return RedirectToAction("AnotherAction");
}

     throw new HttpException(403, "Something happened");

    } 

    public ActionResult Dashboard()
    {
ViewData("test") = "Hello World";
         return View();
    } 

如果您还没有要测试视图正在呈现的内容,请在您的仪表板视图中放置 @ViewData("test")。

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 2014-01-26
    • 2023-03-11
    • 1970-01-01
    • 2016-07-29
    • 2019-07-17
    • 1970-01-01
    • 2021-08-04
    相关资源
    最近更新 更多