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.
我正在寻找一种可靠的技术,将Ajax添加到工作的ASP.NET MVC应用程序中。我想使用jQuery,并了解如何使用jQuery的AJAX功能。
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.
我需要知道的是我应该如何编写我的控制器以便我可以在没有JavaScript的情况下运行该站点,但同时可以进行AJAX调用而无需单独的视图,单独的控制器或任何类型的路由入侵。我的目标是在启用JavaScript时增强工作应用程序,而无需复制或重新创建应用程序的元素。
2 个解决方案
#1
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.
通常,您会创建自己的网站,以便在不启用JavaScript的情况下运行。然后,您将添加使用Ajax增强您的网站所需的不显眼的JavaScript,例如为链接,表单提交等添加事件处理程序以生成GET / POST请求并相应地更新UI。
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.
您在MVC应用程序中需要的唯一更改是处理Ajax请求并将数据作为JSON,XML等返回。
#2
0
in your controller (derived from Controller), you can call Request.IsMvcAjaxRequest() to check if the request is a normal POST or an AJAX request. This will be true if the request was created from a an AjaxForm submit or an AsyncHyperlink. The Ajax form can be made visible by javascript, along with hiding the standard form.
在您的控制器(从Controller派生)中,您可以调用Request.IsMvcAjaxRequest()来检查请求是正常POST还是AJAX请求。如果请求是从AjaxForm提交或AsyncHyperlink创建的,则会出现这种情况。 Ajax表单可以通过javascript显示,同时隐藏标准表单。
#1
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.
通常,您会创建自己的网站,以便在不启用JavaScript的情况下运行。然后,您将添加使用Ajax增强您的网站所需的不显眼的JavaScript,例如为链接,表单提交等添加事件处理程序以生成GET / POST请求并相应地更新UI。
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.
您在MVC应用程序中需要的唯一更改是处理Ajax请求并将数据作为JSON,XML等返回。
#2
0
in your controller (derived from Controller), you can call Request.IsMvcAjaxRequest() to check if the request is a normal POST or an AJAX request. This will be true if the request was created from a an AjaxForm submit or an AsyncHyperlink. The Ajax form can be made visible by javascript, along with hiding the standard form.
在您的控制器(从Controller派生)中,您可以调用Request.IsMvcAjaxRequest()来检查请求是正常POST还是AJAX请求。如果请求是从AjaxForm提交或AsyncHyperlink创建的,则会出现这种情况。 Ajax表单可以通过javascript显示,同时隐藏标准表单。