Jquery Ajax + ASP MVC 3 Forms
I am currently writing a simple web app which needs to update form changes via Ajax requests.
I did some Ajax based calls in MVC1 but haven't touched it since, and I believe we were using MicrosoftAjax not JQuery. So I have read that Microsoft is currently going with JQuery and dropping MicrosoftAjax, and im a bit confused as to how I get my form to communicate with my mvc action. There is also talk of Unobtrusive Javascript, which seems to relate to Ajax calls, but im finding it all a tad confusing getting a solid example of how to use Jquery with MVC3 to call an action and return a result...
public AjaxController : Controller
{
public ActionResult Index()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AjaxAction(SomeModel model)
{
//...Do Stuff
}
}
<html>
<head>
开发者_运维问答 </head>
<body>
<!-- Form goes here -->
</body>
</html>
So should I be using Html.FormBegin, or AjaxBegin etc?
Updating to MVC3 with jQuery is quite simple.
There is an upgrade tool for MVC1-> MVC2: http://weblogs.asp.net/leftslipper/archive/2010/03/10/migrating-asp-net-mvc-1-0-applications-to-asp-net-mvc-2-rtm.aspx
And a tool to upgrade from MVC2 -> MVC3 http://blogs.msdn.com/b/marcinon/archive/2011/01/13/mvc-3-project-upgrade-tool.aspx
You can also read the release document to see how to manually upgrade.
For the AJAX part, once you put the right files into your project (Create a new MVC3 project, and copy all the javascript files to your current project.) and reference those scripts instead of MSAjax, you should be ready to go.
The unobtrusive part, is also super easy. See http://yobriefcase.posterous.com/unobtrusive-javascript-in-mvc3
Basically just add some settings in your web.config, and reference the right javascripts, and your done.
In the end, you probably don't have to change anything in your code, just reference the right files and you'r done.
精彩评论