Use the PartialView method in ASP. NET MVC

Source: Internet
Author: User

We know that, as a response to an Action, the most common practice is return View (); that is, return a View. However, if an operation is only part of the page to be returned, a typical scenario is to implement partial refresh on the page. We previously used javascript to parse json data to implement the partial refresh function. In the following example, the code [csharp] // <summary> /// returns the current comment of a photo /// </summary> /// <returns> </returns> // [AcceptVerbs (HttpVerbs. post)] [Authorize] public ActionResult Blog (string id) {var blogs = new [] {new {Title = "comment Title", Details = "my comments ", author = "Chen xizhang", Time = DateTime. now. toString ()}, new {Title = "comment Title", Details = "My comment", Author = "Chen xizhang", Time = DateTime. now. toString ()}, new {Title = "comment Title", Details = "my comments", Author = "Chen xizhang ", Time = DateTime. now. toString ()}, new {Title = "comment Title", Details = "My comment", Author = "Chen xizhang", Time = DateTime. now. toString ()}, new {Title = "comment Title", Details = "My comment", Author = "Chen xizhang", Time = DateTime. now. toString () }}; return Json (blogs, "text/json");} javascript code on the page [javascript] $. ajax ({type: "POST", url: action + key, dataType: "json", success: function (result) {$ ("# blog "). empty (); var ol = $ ("<ol/>"); $. each (result, fun Ction (I, n) {var t = n. title + ", (" + n. author + "), created at:" + n. time + "<div>" + n. details + "</div>"; $ ("<li/> "). append (t ). appendTo (ol) ;}); ol. appendTo ($ ("# blog") ;}}); this can indeed implement our functions, but it is too cumbersome, and because we need to splice those divs in js, it is very error-prone. A better way is to create a PartialView, which is actually a UserControl [html] <% @ Control Language = "C #" Inherits = "System. web. mvc. viewUserControl <IEnumerable <Web. models. blogItem> "%> <table> <tr> <th> Ttile </th> <th> Author </th> <th> Details </th> <th> Time </th> </tr> <% foreach (var item in Model) {%> <tr> <td> <% = Html. encode (item. title) %> </td> <% = Html. encode (item. author) %> </td> <% = Html. encode (item. details) %> </td> <% = Html. encode (item. time) %> </td> </tr> <% }%> </table> and, modify the Action Code [csharp] /// <summary> /// returns the current comments of a photo /// </summary> /// <returns> </returns> // [AcceptVerbs (HttpVerbs. post)] [Authorize] public ActionResult Blog (string id) {var blogs = new Models. blogItem [] {new Models. blogItem () {Title = "comment Title", Details = "My comment", Author = "Chen xizhang", Time = DateTime. now}, new Models. blogItem () {Title = "comment Title", Details = "My comment", Author = "Chen xizhang", Time = DateTime. now}, new Models. blogItem () {Title = "comment Title", Details = "My comment", Author = "Chen xizhang", Time = DateTime. now}, new Models. blogItem () {Title = "comment Title", Details = "My comment", Author = "Chen xizhang", Time = DateTime. now}, new Models. blogItem () {Title = "comment Title", Details = "My comment", Author = "Chen xizhang", Time = DateTime. now }}; return PartialView ("BlogView", blogs) ;}in this case, in js, you only need a sentence [html] $ ("# blog "). load (action + key );

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.