简体   繁体   English

JQuery和Razor控制器查看ActionResult

[英]JQuery and Razor Controller View ActionResult

I have a controller who can return a Success or Error page like this: 我有一个可以返回成功或错误页面的控制器,如下所示:

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

Those Success and Error pages contains only basic text and are displayed in the Shared/_Layout.cshtml. 这些成功和错误页面仅包含基本文本,并显示在Shared / _Layout.cshtml中。

In my js I want to call those pages defined by the return View, but how can I do that ? 在我的js中,我想调用由return View定义的那些页面,但是我该怎么做呢?

I tested : window.location.reload(); 我测试了: window.location.reload();
Which works but it only reloads the actual Index page. 可以,但是只能重新加载实际的索引页面。
If I try : window.location.href = data.url; 如果我尝试: window.location.href = data.url;
It fails because the page http://xxx/xxx/File_post doesn't exists. 由于页面http:// xxx / xxx / File_post不存在而失败。
And if I do : $('#main').html(data); 如果我这样做: $('#main').html(data);
The page have the good looking but the content is empty. 该页面外观不错,但内容为空。

Edit: I am using jquery.fileupload so I have : 编辑:我正在使用jquery.fileupload所以我有:

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

and

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

In my jqXHR.reponseText and data.result there is the good "Success" or "Error" html so I think I need to fill the page with this but how ? 在我的jqXHR.reponseText和data.result中,有很好的“ Success”或“ Error” html,因此我认为我需要用此填充页面,但是如何?

Any ideas ? 有任何想法吗 ? thanks a lot ! 非常感谢 !

I found how to do it. 我找到了怎么做。 As I have in my Layout a <div id="main"> I can use my data.result to fill the page with my "Success" or "Error" message. 正如我在版式中有一个<div id="main">我可以使用data.result来用“成功”或“错误”消息填充页面。 So I have : 所以我有 :

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

And

return PartialView("Success");

Now the page is correctly displayed. 现在,该页面已正确显示。

You can try with this code 您可以尝试使用此代码

$.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());
});

And in your View you can add this code 在您的视图中,您可以添加此代码

var Settings= {
    YourUrl: '@Url.Action("Action","Controller")'
}

The current action can handle only POST requests. 当前操作只能处理POST请求。 So you can create another action to return the view for GET request. 因此,您可以创建另一个操作以返回GET请求的视图。

Ex. 例如

public ViewResult Success()
{
   return View();
}

you can changed the statusCode to 500 or error code you need; 您可以将statusCode更改为500或所需的错误代码;

C# C#

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM