jquery - ASP.NET MVC: The best way to return type to a AjaxRequest? -


i have multiple actionresults in controller. of them process either ajaxrequest , normal requests, dependending on request (duh!). point is, if i'm adding database using ajaxrequest, want return ok or error (or 1 or 0, etc..) page instead of view() or parcialview() because handle via ajax on client , need yes or no response (or other basic response).

if have normal request (not ajax), it's fine because i'll either redirect controller or return simple view().

so question is: best way return simple value view when processing ajaxrequest()??

// logic insert db (just 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:);  } 

to return plain text view:

return content("ok"); 

or use json alternatively:

return json(new { result = true }); 

update:

jquery automatically detect server sending json response , pass success callback object contain serialized properties:

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

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -