0

我有一个控制器可以返回这样的成功或错误页面:

[HttpPost]
public ActionResult File_post(HttpPostedFileBase file)
{
   if (...)
      return View("Success");
   else
      return View("Error");
}

这些成功和错误页面仅包含基本文本并显示在 Shared/_Layout.cshtml 中。

在我的 js 中,我想调用返回视图定义的那些页面,但我该怎么做呢?

我测试了:window.location.reload();
哪个有效,但它只会重新加载实际的索引页面。
如果我尝试:window.location.href = data.url;
它会失败,因为页面http://xxx/xxx/File_post不存在。
如果我这样做:$('#main').html(data);
页面看起来不错,但内容是空的。

编辑:我正在使用 jquery.fileupload 所以我有:

 <input id="fileupload" type="file" name="file" />

$('#fileupload').fileupload(
{
  done: function (e, data) {
    // Use the return View("Success")
  },
  fail: function (e, data) {
    // Use the return View("Error")
  }
});

在我的 jqXHR.reponseText 和 data.result 中有很好的“成功”或“错误”html,所以我想我需要用这个填充页面但是如何?

有任何想法吗 ?多谢 !

4

4 回答 4

1

我找到了怎么做。正如我在布局中所做的<div id="main"> 那样,我可以使用我的 data.result 用我的“成功”或“错误”消息填充页面。所以我有 :

done: function (e, data) {
  $('#main').html(data.result);
}

return PartialView("Success");

现在页面已正确显示。

于 2012-09-28T09:45:34.653 回答
0

您可以尝试使用此代码

$.ajax({
 type: "POST",
 url: Settings.YourUrl,
 data: "{queryString:'" + searchVal + "'}",
 contentType: "application/json; charset=utf-8",
 dataType: "html",
 success: function (data) {
 alert("here" + data.d.toString());
});

在您的视图中,您可以添加此代码

var Settings= {
    YourUrl: '@Url.Action("Action","Controller")'
}
于 2012-09-27T16:48:22.193 回答
0

当前操作只能处理 POST 请求。因此,您可以创建另一个操作来返回 GET 请求的视图。

前任。

public ViewResult Success()
{
   return View();
}
于 2012-09-27T16:51:22.717 回答
0

您可以将 statusCode 更改为 500 或您需要的错误代码;

C#

[HttpPost]
public ActionResult File_post(HttpPostedFileBase file)
{
   if (...) 
   {
      return View("Success");
   }
   else
   {
      Response.StatusCode = 500;
      Response.TrySkipIisCustomErrors = true; 
      return View("Error");
   }
}

JS:

$('#fileupload').fileupload(
{
  done: function (e, data) {
    // Use the return View("Success")
  },
  fail: function (e, data) { // http status response != 200
    // Use the return View("Error")
  }
});
于 2012-09-28T07:54:08.900 回答