ASP.NET MVC:将类型返回到AjaxRequest的最佳方法?

时间:2022-08-26 20:49:23

I have Multiple ActionResults in my Controller. Almost all of them process either AjaxRequest and normal requests, dependending on the request (duh!). The point is, if I'm adding something to the database using an AjaxRequest, I just want to return a OK or ERROR (or 1 or 0, etc..) to my page instead of a View() or a ParcialView() because I will handle via ajax on the client and I just need a yes or no response (or any other basic response).

我的Controller中有多个ActionResults。几乎所有这些都处理AjaxRequest和正常请求,依赖于请求(呃!)。关键是,如果我使用AjaxRequest向数据库添加内容,我只想将OK或ERROR(或1或0等等)返回到我的页面而不是View()或ParcialView()因为我将通过客户端上的ajax处理,我只需要一个是或否响应(或任何其他基本响应)。

If I have a normal request (not ajax), it's fine because I'll either redirect to another controller or return a simple View().

如果我有一个正常的请求(不是ajax),那很好,因为我将重定向到另一个控制器或返回一个简单的View()。

So the question is: what is the best way to return a simple value to my view when processing AjaxRequest()??

所以问题是:在处理AjaxRequest()时,将简单值返回到视图的最佳方法是什么?

// logic to insert into the db (just an example)
result = Person.Add();

if(Request.IsAjaxRequest()) {

   if(result == ok)
      return true;
   else
      return false;
 }
 // Normal Request
 else {

   if(result == ok)
      return Redirect("PersonList");
   else
      return View("Error:);
 }

3 个解决方案

#1


4  

To return plain text to the view:

要将纯文本返回到视图:

return Content("ok");

Or use JSON alternatively:

或者使用JSON:

return Json(new { result = true });

UPDATE:

jQuery will automatically detect that the server is sending JSON response and it will pass to the success callback an object that will contain the serialized properties:

jQuery将自动检测服务器是否正在发送JSON响应,它将向成功回调传递一个包含序列化属性的对象:

$.ajax({ 
    type: "POST", 
    data: theForm.serialize(), 
    url: theForm.attr('action'), 
    success: function(json) { 
        alert(json.result); // result is the property name used in the controller
    },
    error: function() { 
        alert('Error!'); 
    } 
});

#2


0  

If this is standard behavior in your entire application, you might want to look into creating an ActionFilter that encapsulates your ajax logic.

如果这是整个应用程序中的标准行为,您可能希望研究创建一个封装您的ajax逻辑的ActionFilter。

#3


0  

I found the problem. JQuery doesn't detect automatically the return type, so you need to specify using the 'dataType' property. Look at the code below. It worked perfectly! Thanks you all.

我发现了问题。 JQuery不会自动检测返回类型,因此您需要使用'dataType'属性进行指定。看下面的代码。它工作得很好!谢谢大家。

 $.ajax({
         type: "POST",
         data: theForm.serialize(),
         url: theForm.attr("action"),
         dataType: "json", // Choose the return type - json
         success: function(json) {
              alert(json.returnCode);
         },
         error: function() {
              alert('Error!');
         }
       });

#1


4  

To return plain text to the view:

要将纯文本返回到视图:

return Content("ok");

Or use JSON alternatively:

或者使用JSON:

return Json(new { result = true });

UPDATE:

jQuery will automatically detect that the server is sending JSON response and it will pass to the success callback an object that will contain the serialized properties:

jQuery将自动检测服务器是否正在发送JSON响应,它将向成功回调传递一个包含序列化属性的对象:

$.ajax({ 
    type: "POST", 
    data: theForm.serialize(), 
    url: theForm.attr('action'), 
    success: function(json) { 
        alert(json.result); // result is the property name used in the controller
    },
    error: function() { 
        alert('Error!'); 
    } 
});

#2


0  

If this is standard behavior in your entire application, you might want to look into creating an ActionFilter that encapsulates your ajax logic.

如果这是整个应用程序中的标准行为,您可能希望研究创建一个封装您的ajax逻辑的ActionFilter。

#3


0  

I found the problem. JQuery doesn't detect automatically the return type, so you need to specify using the 'dataType' property. Look at the code below. It worked perfectly! Thanks you all.

我发现了问题。 JQuery不会自动检测返回类型,因此您需要使用'dataType'属性进行指定。看下面的代码。它工作得很好!谢谢大家。

 $.ajax({
         type: "POST",
         data: theForm.serialize(),
         url: theForm.attr("action"),
         dataType: "json", // Choose the return type - json
         success: function(json) {
              alert(json.returnCode);
         },
         error: function() {
              alert('Error!');
         }
       });