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>