How to Sort the WebGrid in MVC4
Please find the code below.
@{
var query = Request.Url.GetComponents(UriComponents.Query, UriFormat.Unescaped);
var columns = new[] { "FName", "Category", "RReference", "CDate", "UserName" };
(The above columns or my web grid columns.)
var orderBy = "FormName";
if (!Request["sort"].IsEmpty())
{
if (columns.Contains(Request["sort"]))
{
orderBy = Request["sort"];
if (Request["sortdir"] == "DESC")
{
orderBy += " DESC";
}
else if (Request["sortdir"] == "ASC")
{
orderBy += " ASC";
}
}
}
var grid = new System.Web.Helpers.WebGrid
(
source: Model,
canPage: true,
rowsPerPage: 50,
ajaxUpdateContainerId: "MainData"
);
grid.Pager(WebGridPagerModes.NextPrevious);
}
<div id="grid">
<div id="table">
@grid.GetHtml(
tableStyle: "webgrid",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
columns:
grid.Columns
(
grid.Column("FName", "First Name"),
grid.Column("Category", "Category"),
grid.Column("RReference", "Record Reference"),
grid.Column("CDate", "Completed Date", format: @<text>@item.FormCompletedDate.ToString("dd/MM/yyyy")</text>),
grid.Column("UserName", "User Login")
)
)
</div>
</div>
No comments:
Post a Comment