Some people also commented that the logic in the Controller should not process Nikhil differently based on the AJAX request or not. RenderPartial is used to replace RenderView for AJAX operation output ), therefore, this solution undermines the role of MVC. I don't think so, but I hope to do this, because doing so means absolute transparency. Absolute transparency means that the Controller has completely handed over the AJAX decision of an application to the client, which is ideal because AJAX is completely a concept of a presentation layer. The UpdatePanel in ASP. net ajax has a remarkable performance in this aspect, although it is far from perfect). Therefore, I finally decided to develop a component similar to UpdatePanel for ASP. NET MVC. Fortunately, ASP. net mvc uses WebForm pages as view templates by default. Under this powerful model, it does not seem very difficult to build a prototype of such an AJAX solution.
I name this control MvcAjaxPanel. The biggest difference between MvcAjaxPanel and UpdatePanel is that the latter receives PostBack, while the former only receives normal HTTP requests. Post "Back" means that after the Post, the original Page is returned, and ASP. net mvc requests are usually directed to different pages. Therefore, how to update content across pages is the primary solution of MvcAjaxPanel. Finally, I chose to specify an UpdateAreaID for each MvcAjaxPanel.
- <mvc:MvcAjaxPanel runat="server" ID="mvcAjaxPanel" UpdateAreaID="Header">
- ...
- </mvc:MvcAjaxPanel>
When a page sends an AJAX request to the server, the UpdateAreaID information in the page is attached, and the server Action does not realize this, therefore, a view template is still specified according to the common logic and HTML is output. However, if the MvcAjaxPanel in the View template finds that this request is actually an agreed AJAX request, please note that only the View component realizes that this is the nature of the request ), the new method is used to replace the standard output. In this case, the template will find the MvcAjaxPanel with the same attribute value based on the UpdateAreaID passed by the client and output the content selectively. On the client, the corresponding JavaScript code will receive data from the server and update the corresponding area on the page.
Obviously, MvcAjaxPanel has many similarities with UpdatePanel, and is transparent to some extent. In addition, compared with the Nikhil solution, a very important advantage is that multiple regions in the page can be updated at a time-in fact, this is one of the characteristics of UpdatePanel. In addition, this method of transparency to the Controller has a natural feature, that is, the ability to easily switch pages in browsers that do not support AJAX.
- AsyncState parameter of ASP. NET
- ASP. net mvc executes asynchronous Action
- Overview ASP. net mvc Framework
- Use the UpdataModel method in ASP. NET MVC
- ASP. net mvc Action Method