6

I am looking for a reliable technique for adding Ajax to a working ASP.NET MVC application. I want to use jQuery, and understand how to use the AJAX functionality from jQuery.

What I need to know is how I should write my controller so that I can run the site without JavaScript, but at the same time make AJAX calls possible without the need for a separate view, separate controller, or any kind of route hack. My goal is to have a working application enhanced when JavaScript is enabled without the need to duplicate or recreate elements of the app.

4

2 回答 2

1

Typically you would create your site so that it works without JavaScript being enabled. Then you would add the unobtrusive JavaScript needed to enhance your site with Ajax e.g. adding event handlers for links, form submits, etc. to make GET / POST requests and update your UI accordingly.

The only changes you would need in your MVC app would be to handle the Ajax requests and return the data as JSON, XML, etc.

于 2008-09-11T12:51:03.377 回答
0

在您的控制器(从 Controller 派生)中,您可以调用 Request.IsMvcAjaxRequest() 来检查请求是普通 POST 还是 AJAX 请求。如果请求是从 AjaxForm 提交或 AsyncHyperlink 创建的,则情况会如此。javascript 可以使 Ajax 表单可见,同时隐藏标准表单。

于 2009-01-11T04:53:01.710 回答