开发者

ASP.NET MVC displaying user Data such as comments?

hows the best way to display comments/user data from a db using mvc?

do we just do our own for loop and manually display it or? will doing it this way cause paging problems in the future? any best methods or practice?

jq开发者_如何学运维uery?

thanks guys.


When I first started learning MVC, I put a lot of conditional code in the view to manage the display of the data and controls. However, once I figured out the benefit of HTML helpers, I found that it not only reduced the code in my views, but it also made it easier to test the resulting output.

As an example, this is a code fragment for an entry form that would appear in the finished HTML page:

<table class="listing">
    <tr>
         <td class="prompt">Last Name:</td>
         <td><input id="LastName" name="LastName" value="Smith" /></td>
    </tr>
    <tr>
         <td class="prompt">First Name:</td>
         <td><input id="FirstName" name="FirstName" value="John" /></td>
    </tr>
    <tr>
         <td class="prompt">Job Title:</td>
         <td><input id="JobTitle" name="JobTitle" value="Manager" /></td>
    </tr>
</table>

Building the view using standard MVC helpers would result in code that looks something like this:

<table class="listing">
    <tr>
         <td class="prompt">Last Name:</td>
         <td><%= Html.TextBox("LastName", Model.LastName) %></td>
    </tr>
    <tr>
         <td class="prompt">First Name:</td>
         <td><%= Html.TextBox("FirstName", Model.FirstName) %></td>
    </tr>
    <tr>
         <td class="prompt">Job Title:</td>
         <td><%= Html.TextBox("JobTitle", Model.JobTitle) %></td>
    </tr>
</table>

However, I was able to create HTML helpers that allowed me to make the view look like this:

<table class="listing">
    <%= Html.Edit_LastName(Model.LastName) %>
    <%= Html.Edit_FirstName(Model.FirstName) %>
    <%= Html.Edit_JobTitle(Model.JobTitle) %>
</table>

Here are the HTML helpers:

private string Edit_Field(string prompt, string fieldName, string value)
{
    string textBox = helper.TextBox(fieldName, value);

    return string.Format("<tr><td class='prompt'>{0}</td><td>{1}</td></tr>", prompt, textBox);
}

public string Edit_LastName(this HtmlHelper helper, string value)
{
    return EditField("Last Name:", "LastName", value);
}

public string Edit_FirstName(this HtmlHelper helper, string value)
{
    return EditField("First Name:", "FirstName", value);
}

public string Edit_JobTitle(this HtmlHelper helper, string value)
{
    return EditField("Job Title:", "JobTitle", value);
}

This method is not nearly as straightforward as the standard method, but if you have similar controls that are used on multiple views, this approach clearly helps with maintenance. For example, if you want to change the prompt text from "Job Title" to "Position", you just change the prompt string in the Edit_JobTitle method and the change is reflected in every view where that method is called.


is the best way to use client jquery with tablesorter or server side for loop?

I don't think there is a connection between these two. Anyways you have to use an iterator to get all the rows from the database. Then you can use jQuery plugin to view the data in pages.

Here is a nice implementation of this

Table Sorting, Paging and Filtering with jQuery


IMO, use the iterator. I use jquery plugins when I need a display format that is complex. jquery itslef is useful when you need to access/modify dom or make ajax calls. Paging shouldn't affect this decision or vice-versa.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜