ASP. net mvc Framework to save UpdatePanel

Source: Internet
Author: User

What is "Saving UpdatePanel for ASP. net mvc Framework? I believe everyone knows about UpdatePanel in ASP. NET AJAX. Unfortunately, the birth of the ASP. net mvc Framework "destroys" a large number of PostBack-based controls, and the first possibility is UpdatePanel. Without PostBack, UpdatePanel loses all its functions. It is even better to bind some controls, at least they can be used for display. After sighing for UpdatePanel, we began to miss the advantages of UpdatePanel: "Transparent ". With the help of UpdatePanel, AJAX operations are almost completely transparent to developers. We only need to wrap the content that requires AJAX updates with UpdatePanel, and everything is so elegant.

Can we save UpdatePanel in ASP. net mvc? Maybe yes, but it is more like an "impossible task ". I am not the legendary atango, so it seems more feasible to customize an AJAX solution for ASP. NET MVC. Although we will not demand the perfection of a new thing from its birth, even if it is just a prototype, it must strictly abide by the following principles:

Do not destroy protocol collaboration and responsibilities in MVC)

As transparent as possible to developers

Nikhil Kothari once proposed his AJAX solution under ASP. net mvc framework. If you do not know his practice, I will summarize it here first. Nikhil extends the Controller to support an Ajax operation, so we can write the following code in the Code:

 
 
  1. publicclassTaskListController:AjaxController{  
  2. ...  
  3. publicvoidCompleteTask(inttaskID){  
  4. if(String.IsNullOrEmpty(Request.Form["deleteTask"])==false){  
  5. InvokeAction("DeleteTask");  
  6. return;  
  7. }  
  8.  
  9. Tasktask=_taskDB.GetTask(taskID);  
  10. if(task!=null){  
  11. _taskDB.CompleteTask(task);  
  12. }  
  13.  
  14. if(IsAjaxRequest){  
  15. if(task!=null){  
  16. RenderPartial("TaskView",task);  
  17. }  
  18. }  
  19. else{  
  20. RedirectToAction("List");  
  21. }  
  22. }  
  23. ...  

Like AjaxController, Nikhil also provides some extension methods for ViewPage and ViewControl. Therefore, we can see the following code in ViewList. aspx:

 
 
  1. <divid="taskList">  
  2. <%foreach(TasktaskinTasks){%>  
  3. <div>  
  4. <%this.RenderPartial("TaskView",task);%>  
  5. </div>  
  6. <%}%>  
  7. </div> 

Both View and Controller have calls to the RenderPartiel method. Their function is to output an HTML code generated by "Partial Template" to the client. In the default configuration of ASP. net mvc, the Partial Template is User Control. In the Partial Template of TaskView, we can see some auxiliary methods:

 
 
  1. <divid="taskItem<%=Task.ID%>"class="taskPanel">  
  2. <%Ajax.Initialize();%>  
  3. <%this.RenderBeginAjaxForm(  
  4. Url.Action("CompleteTask"),  
  5. new{  
  6. Update="taskItem"+Task.ID,  
  7. UpdateType="replace",  
  8. Completed="endUpdateTask"});%>  
  9.  
  10. <inputtype="hidden"name="taskID"value="<%=Task.ID%>"/>  
  11. <inputtype="submit"class="completeButton"name="completeTask"value="Done!"/>  
  12. <inputtype="submit"class="deleteButton"name="deleteTask"value="Delete"/>  
  13. <span><%=Html.Encode(Task.Name)%></span>  
  14.  
  15. <%this.RenderEndForm();%>  
  16. <%Ajax.RenderScripts();%>  
  17. </div> 


These auxiliary methods are used to generate tags and scripts that trigger AJAX updates. When you click the submit button between the tags generated by RenderBeginAjaxForm and RenderEndForm, the webpage sends an AJAX request to the server, and the server Action finally outputs the HTML generated by a Partial Template through the RenderPartial method. The HTML output by the server end will be replaced or added to an element of the page. This forms an AJAX effect. This solution looks cool in some aspects, especially the generated code can be added to an element, not just as an UpdatePanel replacement, for example, Nikhil uses this feature in his example to implement an add function. However, it seems that this solution is not ideal if we use the previously proposed principles. The above introduces how ASP. net mvc Framework saves UpdatePanel

  1. AsyncState parameter of ASP. NET
  2. ASP. net mvc executes asynchronous Action
  3. Overview ASP. net mvc Framework
  4. Use the UpdataModel method in ASP. NET MVC
  5. ASP. net mvc Action Method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.