Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing MVC4 application (using Razor view) with Searching, Paging and Sorting. I have "User Name" & "Upload Date" column for sorting purpose. Everything works fine But now I want to display sorting order after column name in my view. eg If we click on "User Name" it will be sorted in ascending order for first time & Clicking again on the same column sort data in descending order. But User doesn't get any idea looking at view whether column is sorted or not & in which order.

To render column name as sortable, I used following code in my view
<th>
                @Html.ActionLink("User Name", "Sorting", new
                {
                    sortBy = ViewBag.SortUserNameParameter,
                    searchByUserName = Request.QueryString["searchByUserName"],
                    searchByReaderName = Request.QueryString["searchByReaderName"],
                    searchByReaderType = Request.QueryString["searchByReaderType"],
                    searchByUploadDate = Request.QueryString["searchByUploadDate"]
                })
</th>


Note : It would be great if somehow we manage to display image arrow with upward & downward direction besides column name.

What I have tried:

I am new to MVC development. Googled a lot for this issue but can't find appropriate solution.
Posted
Updated 6-Sep-16 21:35pm
v2

1 solution

You can do this easily by using jquery
<script type="text/javascript">
    $('thead > tr > th > a[href*="sort=@grid.SortColumn"]').parent()
    .append('@(grid.SortDirection == SortDirection.Ascending ? "▲" : "▼")');
</script>

Programmining: Sort indicator in System.Web.Helpers.WebGrid[^]
 
Share this answer
 
Comments
Umesh AP 7-Sep-16 5:02am    
@NaibedyaKar - I am not using webgrid for displaying records. So this approach is not applicable in my case.
Please suggest alternative solution.
NaibedyaKar 7-Sep-16 5:59am    
I have given an example how to do that for webgrid.
You can select the header element of your grid using jquery selectors and append the text to it.
Umesh AP 7-Sep-16 7:03am    
Really sorry, but i can't understand what you are trying to say. I m new to javascript dev.
I don't use any grid instead used html table element for rendering grid. Any other way to achieve the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900