在MVC3索引视图改变顺序 [英] Change ordering in MVC3 Index view

查看:99
本文介绍了在MVC3索引视图改变顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想拥有可点击的列标题例如,通过上标记$ C $单击C一次,它的订单,它再次反转。同样的号码。

Would like to have clickable column titles eg click on TagCode once and it orders by that, and again it reverses. Same for Number.

使用MVC3 /剃刀和光速(ORM)。

Using MVC3/Razor and LightSpeed (ORM).

我知道,一个网格如的http://mvccontrib.$c$cplex.com/ 可能是前进的方向。但正如我不需要传呼或过滤,我想保持简单了。

I know that a grid eg http://mvccontrib.codeplex.com/ may be the way forward. But as I don't need paging or filtering, I'd like to keep it simple for now.

问题有code的一个简单的例子(也许有向上/向下图标),这将有助于?

Problem Is there a simple example of code (maybe with an up/down icon) that would help?

推荐答案

@戴夫

对不起,我错过了你的主要观点在我的第一个答案

如果要实现使用纯MVC3排序,
我可以做到这一点下面的步骤

Sorry, I missed your main point in my first answer
If you want to implement sorting using pure MVC3, I can do that with following steps


  1. 列表操作方法传递sortcolumn和订单信息通过ViewBag到的ViewPage
  2. HTML辅助方法ActionLink的建设与列顺序显示
  3. 列表的ViewPage调用辅助方法
  1. list action method passing sortcolumn and order info to viewpage via ViewBag
  2. Html Helper method to build actionlink with column ordering display
  3. list viewpage calling the helper method

我上传的源$ C ​​$ C此处



列表操作方法

        public ActionResult Index(string sortColumn, bool? asc)
    {
        if (string.IsNullOrWhiteSpace(sortColumn))
            sortColumn = "Number";

        asc = asc ?? true;
        SortDirection sortDirection = asc == true ? SortDirection.Ascending : SortDirection.Descending;
        var query = _service.GetTags().OrderBy(sortColumn, sortDirection);

        return View(query);
    }

HTML辅助方法

        public static MvcHtmlString ActionLinkWithColumnOrder(this HtmlHelper helper,
        string columnName,string action,string currentColumn,bool currentOrder)
    {
        object routeValues;
        object htmlAttributes = null;
        if (columnName == currentColumn)
        {
            routeValues = new { sortColumn = columnName, asc = !currentOrder };
            htmlAttributes = new { @class = currentOrder ? "sort_asc" : "sort_desc" };
        }
        else
        {
            routeValues = new { sortColumn = columnName };
        }

        return helper.ActionLink(columnName, action, routeValues, htmlAttributes);
    }

列表视图页

...

@ Html.ActionLinkWithColumnOrder(变量code,指数,(串)ViewBag.sortColumn,(布尔)ViewBag.asc)

...

@ Html.ActionLinkWithColumnOrder(编号,索引,(串)ViewBag.sortColumn,(布尔)ViewBag.asc)

List View page
...
@Html.ActionLinkWithColumnOrder("TagCode", "Index", (string)ViewBag.sortColumn, (bool)ViewBag.asc)
...
@Html.ActionLinkWithColumnOrder("Number", "Index", (string)ViewBag.sortColumn, (bool)ViewBag.asc)





快乐Mvcing!

Sangsu PARK( HTTP://su$p$pmeware.blogspot.com

Sangsu PARK (http://supremeware.blogspot.com)

这篇关于在MVC3索引视图改变顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆