1

我在网格中有以下信息返回,但它不会先按名字排序记录,然后按姓氏排序。请让我知道如何实现这一目标。我目前正在使用 MVC4 和 kendoui 网格

public ActionResult Index()
    {
        return View();
    }

public ActionResult GetSession([DataSourceRequest] DataSourceRequest request)
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    return Json(GetAllSessionList().ToDataSourceResult(request),         JsonRequestBehavior.AllowGet);
} 
 private static IEnumerable<ListsViewModel> GetAllSessionList()
    {
        var context = new HEntities();

        return context.vwSessionListConfigs
            .OrderBy(x => x.FirstName).ThenBy(x => x.FamilyName)
            .Select(session => new ListsViewModel
            {
                ConsumerID = session.ConsID,
                ConsumerHCID = session.ConsHCL,
                ConsumerHRN = session.ConsHRK,
                ConsumerFirstName = session.FirstName,
                ConsumerFamilyName = session.GivenName,
                ConsumerGender = session.Gender,
            });
    }

看法:

@(Html.Kendo().Grid<Web_App.ViewModel.ListsViewModel>()

    .Name("SList")
    .HtmlAttributes(new { @Style = "align:center; font-size:10px;" })
    .Columns(columns =>
    {
        columns.Bound(p => p.ConsID).Visible(false);
        columns.Bound(p => p.ConsHCL).Width(80);
        columns.Bound(p => p.ConsHRK).Width(50);
        columns.Bound(p => p.FirstName).Width(80);
        columns.Bound(p => p.GivenName).Width(80);
        columns.Bound(p => p.ConsumerAlias).Width(45);
        columns.Bound(p => p.Gender).Width(30);

        columns.Command(commands => commands.Edit()).Width(175);      
    })
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .Pageable()
    .Selectable(s => s.Mode(Kendo.Mvc.UI.GridSelectionMode.Single))
    .Scrollable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(10)
        .ServerOperation(true)
        .Model(model => model.Id(p => p.ConsID))
        .Read(read => read.Action("GetSession", "Manage").Type(HttpVerbs.Get))
        .Update("Edit", "Manage")         
    )
    .Events(events => events
                            .Change("change"))



)
4

1 回答 1

3

在将集合传递给 ToDataSourceResult 方法之前如何对集合进行排序并不重要。ToDataSourceResult 方法将在内部再次按模型的第一个属性对集合进行排序。

要对您的集合进行排序,您应该使用数据源的 Sort 方法。

.DataSource(dataSource => dataSource
            .Ajax()
            .Sort(st => {

                st.Add(m => m.Name).Descending();
                st.Add(m => m.PersonID).Descending(); 
            })
于 2012-10-30T19:57:53.040 回答