user887983
user887983

Reputation:

MVC3 Webgrid sorting problem with 2nd+ columns

in this sample the sorting of the grid works well with the first column. Every other column can only be sorted ascending, the "sortdir" never switches to "DESC". (Ajax caching is also disabled)

Does anybody knows a solution or had the same problem? What im doing wrong?

Controller:

[OutputCache(Location = OutputCacheLocation.None)]
public ActionResult Index_Result_Org(string sort, string sortdir)
{
   this.setRep();
   this.rep.LoadOV();
   return View("Index_OV", rep.GetOV(sort != null ? sort : "Kennung", sortdir != null ? sortdir == "ASC" : true));
}

View:

@model List<Models.OV_View>
@{
    Layout = null;
    var grid_BA = Html.Grid<OV_View>(Model, ajaxUpdateContainerId: "BAS_OV", canPage: false, defaultSort: "Kennung");        
}
<div id="BAS_OV">
    @grid_BA.GetHtml(
    htmlAttributes: new { @id = "webgrid_BA" },
    alternatingRowStyle: "alt",
    tableStyle: "BAS",
    columns: grid_BA.Columns(
                         grid_BA.Column("Kennung", header: "Verbandskennung", format: @<text>@Html.Label(@item.Data.Kennung)</text>, canSort: true),
                         grid_BA.Column("Name", header: "Verbandsname", format: @<text>@item.Data.Name</text>, canSort: true),
                         grid_BA.Column("Anzahl", header: "Anzahl", format: @<text>@item.Data.Anzahl</text>, canSort: true, style: "counter_column"),
                         grid_BA.Column("Select", header: "X", canSort: false, format: @<text><input id="Select" name="Select" type="checkbox" onclick="Select(this)" value="@item.Select" @(item.Select == true ? "Checked" : null) /></text>, style: "checkbox_column"),
                         grid_BA.Column("ID", "", format: @<text>@item.Data.ID</text>, canSort: false, style: "invisible_column")
                            )
                      )
</div>

Upvotes: 0

Views: 2424

Answers (1)

user887983
user887983

Reputation:

Got it.

Only the defaulSort: Column can be sorted descending.

So i added the following line to the controller:

this.ViewBag.Sort = sort;

And andded and changed the following lines in the view:

var grid_BA = Html.Grid<ErgoBAS_OV_View>(Model, ajaxUpdateContainerId: "BAS_OV", canPage: false, defaultSort: "Kennung");    

to:

string temp = this.ViewBag.Sort != null ? this.ViewBag.Sort : "Kennung";
var grid_BA = Html.Grid<ErgoBAS_OV_View>(Model, ajaxUpdateContainerId: "BAS_OV", canPage: false, defaultSort: temp); 

This is the dirty solution, think to be clean the defaultSort has to be added to the View Model, thats what i will do now.

Upvotes: 1

Related Questions