I'm implementing sorting for a table in my view, where it gets populated through ajax calling JsonResult method
this is the header link I use for ordering:
<a style="cursor: pointer" onclick="getData('@Html.Raw(ViewBag.Sort == "asc" ? "desc" : "asc")')" id="sort">Title</a>
the JsonResult method:
public JsonResult BooksData(string sort)
{
ViewBag.Sort = ViewBag.Sort == "desc" ? "asc" : "desc";
var books = new List<Book>();
if (sort == "asc")
{
books = db.Books.Include(b => b.Author).OrderBy(b => b.Title).ToList();
ViewBag.Sort = "desc";
}
else
{
books = db.Books.Include(b => b.Author).OrderByDescending(b => b.Title).ToList();
ViewBag.Sort = "asc";
}
return Json(books, JsonRequestBehavior.AllowGet);
}
the getsData function:
function getData(sort) {
var srt = sort;
$('#tbl>tbody').empty();
$.ajax({
type: 'GET',
url: '/Book/BooksData?sort=' + srt,
dataTtype: 'json',
success: function (data) {
$.each(data, function (index, val) {
$('#tbl>tbody').append('<tr><td>' + val.Title + '</td><td>' + val.Author.Name + '</td></tr>')
});
}
});
}
but the value of ViewBag.Sort is always asc?
There are numerous issues with your code. First
@Html.Raw(ViewBag.Sort == ...)is razor code and is evaluated on the server before the view is sent to the client so your generatingor
getData("desc")depending on the value ofViewBag.Sortin the GET method that initially generated the view. No where do you ever change it so you ajax call always submits the same value for the parametersort.Then in the
BooksData()method, you have logic errors (although they ultimately do not matter). Because you have not previous set a value forViewBag.Sort, thenevaluates to
so the value of
ViewBag.Sortis always"desc"which yourelseblock then resets to"asc".But your method returns a
JsonResult, not a view, so theViewBagvalue is then lost and its value is never returned to the client.Change your code in the view to
and assign the initial value of
ViewBag.Sortto a javascript variable so it can be toggled each time you click the linkand the controller method is then
Note that you can do the 'sorting' on the client without the extra overhead of making call to the server by simply re-ordering the rows in the table using javascript/jquery - refer Invert table rows for an example, although that means that any new records added to the database by other users after the view is first generated would not be updated in the view.