With ASP.NET MVC, what is the preferred way to Ajaxify a simple form? With ASP.NET MVC, what is the preferred way to Ajaxify a simple form? ajax ajax

With ASP.NET MVC, what is the preferred way to Ajaxify a simple form?


I usually do something like

<form action="/controller/action" method="post" id="formID"></form>$("#formID").submit(function(){  var form = $(this);  $.post(form.attr("href"), form.serialize(), function(data){/*manipulate page*/}, "text")  return false;});

The MVC controller is just like it would be if you weren't using Ajax, except you probably return a different view--one that doesn't contain a full page of markup, depending on what data you want to get from the server. You might not need to return any data at all--your "manipulate page" code might have all the info it needs already.


You should check out the jQuery Form plugin. You can POST the comment to your controller with ajax, persist the comment, and return a bool w/ JSON. In jquery, define a success handler (see the examples tab) and render the comment at the bottom of your page. You can also check out javascript templates (here is Microsoft's proposed implementation) if you want some formatting control over how to render the comment (without cluttering your JS).


There's a helper method that allows you to create an Ajax-enabled form. This article should help you get started:

http://msdn.microsoft.com/en-us/library/dd381533.aspx