Self-taught MVC (14): How to Use tables to display json arrays in views

Source: Internet
Author: User
At the beginning, I loop through the array in the action and concatenate a string with HTML format. Later I thought that the data is too coupled with HTML, and the HTML style cannot be modified on the interface.

So I changed the method. The action is only provided to the front-end json array. The front-end uses an html template and cyclically fills in the json data in the template.

The core code is as follows:

// Get comments
Function getComments (pageindex ){
$. Post ("/Comment/GetCommentsByMID", {Mid: <% = Model. Mid %>, PageIndex: pageindex, PageSize: 10 },
Function (data, textStatus ){
$ ("# Videobodycommentlist"). text (""); // videobodycommentlist is a DIV used to display data.
$. Each (data. result, function (I, n) {// List <Comment> generic set is put in data. result, which is encapsulated in Json.
Var row = $ ("# commentTemp"). clone (); // commentTemp is a template
Row. find ("# commentUser"). text (n. CommentUser. Uname );
Row. find ("# commentTime"). text (renderTime (n. Commenttime ));
Row. find ("# commentContent" pai.html (n. Commentcontent );
Row. show ();
Row. appendTo ("# videobodycommentlist ");
});
}, "Json ");
}

 

The template's HTML code is as follows:

<Div id = "commentTemp" class = "comment" style = "display: none">
<Div class = "bar"> <span id = "commentUser"> </span> published on <span id = "commentTime"> </span> </div>
<Div id = "commentContent"> </div>
</Div>

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.