asp.net MVC 3的WebGrid整理遗体?sortdir = ASC [英] asp.net mvc 3 webgrid sorting remains ?sortdir=ASC

查看:136
本文介绍了asp.net MVC 3的WebGrid整理遗体?sortdir = ASC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试验了一下,在asp.net mvc的几个网格。
微软有一个网格太了解MVC中的$ P $租赁前3,所以我想我会尽力,一出。

I'm experimenting a bit with several grids in asp.net mvc. Microsoft has a grid too know in the prerelease of mvc 3 so I thought I'll try that one out.

的基本功能是很容易实现的,但是当涉及到​​排序,我有一些问题。
网格由URL处理它的排序。在URL中有排序列,并以下列方式排序方向:
  ?sortdir = ASC&安培;排序= ABONNEMENT

The basic functionality is quite easy to implement but when it comes to sorting, I'm having some problems. The grid handles its sorting by the url. In the url you have the sort column and the sort direction in the following way: ?sortdir=ASC&sort=ABONNEMENT

现在你会期望你做的某些列排序后,该列的查询字符串sortdir会改变什么?sortdir = DESC
但事实并非如此。它保持?sortdir = ASC。
有谁知道,如果这是一个错误或特性,以及如何解决这个问题?

Now you would expect that after you've done the sorting on the certain column, the sortdir querystring on that column would change to ?sortdir=DESC but it doesn't. It stays ?sortdir=ASC . Does anybody knows if this is a bug or a feature, and how to solve this?

另一个非常讨厌的事:如果我点击排序链接,一个HTTPGET请求完成。
正因为如此我失去我的模型。因为在页面过滤电网(搜索功能)上的可能性,我想preserve这个模型。
这将是一个更容易与放大器;在我看来,清洁的解决方案把这个数据模型的状态,而不是将其存储在会话状态。
是否有可能改变所以执行HTTP POST排序头链接的行为?

Another very annoying thing: if I click on a sort link, an httpget request is done. Because of this I loose my model. Because there is the possibility on the page to filter the grid (search functionality), I want to preserve this in the model. It would be a much easier & cleaner solution in my opinion to put this data in the model state, than to store it in the session state. Is it possible to change the behaviour of the sorting header links so a http post is executed?

在此的任何意见或想法?
TNX的帮助。

Any ideas or thoughts on this? Tnx for the help.

格尔茨,公园

视图的code是以下内容:

The code of the view is the following:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<table>
    <tr>
        <td>
            <h1 class="normal">List of subscription</h1>
        </td>
        <td>

        </td>
    </tr>
</table>
<% using (Html.BeginForm("List", "Subscription", FormMethod.Post)) { %>

<table border="0" cellpadding="0" cellspacing="5">
    <tr>
        <td>
            Search By
        </td>
        <td>
            <%: Html.DropDownListFor(m => m.SearchByColumn, Ogone.FrontEnd.Web.Controllers.SubscriptionController.SubscriptionSearchList) %>
        </td>
        <td>
            <%: Html.TextBoxFor(m => m.SearchByText) %>
        </td>
        <td>
            <input name="button" type="submit" value="Search" />
        </td>
    </tr>
</table>

<div>
<%  
var grid = new System.Web.Helpers.WebGrid(Model.SubscriptionList,  
              columnNames: new List<string>(){"Title"},  
              canPage:false);  
 %> 
<%= grid.GetHtml(columns: grid.Columns(
             grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.ID })),
             grid.Column("ID"),
             grid.Column("ISP"),
             grid.Column("ABONNEMENT"),
             grid.Column("MODE"),
             grid.Column("SETUPFEE"),
             grid.Column("MONTHLYFEE"))
             ) %>
</div>

推荐答案

这是因为格列名必须符合您的字段或属性。这在网格标题生成URL方法之类的从URL查询字符串有界柱,并与格列名比较。他们三人必须是相同的。如果您的列名的定义不正确配置,URL将无法正确生成。

This happens because grid column names must correspond to your fields or properties. Method which generates url in grid header compares "sort" from url query string with bounded columns and with grid column name. The three of them must be the same. If definition of your column name is not configured properly, the url will not be generated correctly.

..反正这里是正确定义的列名的例子。

Anyway .. here is some examples of properly defined column names.

在这里,我们有样品域类显示

Here we have sample domain class to display

public class Person
{
    public string FirstName;
    public string LastName;
    public Address LivesIn;
}

public class Address
{
    public string Country;
    public string City;
}

现在可以显示列表

使用您的列名的字段

grid.Column("FirstName"),
grid.Column("LastName"),
grid.Column("LivesIn.City"),
grid.Column("LivesIn.Country")

...所有列具有正确的排序网址

... all columns has correct sorting url

如果您在列名称拼写错误你有例外

If you make a typo in column name you got exception

grid.Column("FirstName"),
grid.Column("MyLastName"), <-- this throws exception
grid.Column("LivesIn.City"),
grid.Column("LivesIn.Country")

但是你可以使用格式,不会有异常抛出

But you can use format and there will be no exception thrown

grid.Column("FirstName"),
grid.Column("MyLastName", format: item => item.LastName</text>),
grid.Column("LivesIn.City"),
grid.Column("LivesIn.Country")

...但列MyLastName排序URL会坏!所有的时间sortDir = ASC

... but sorting url for column MyLastName will be BAD !!! all the time sortDir=ASC

您需要用良好的列名有适当的分类网址和自定义格式,所以...

You need to use good column name to has proper sorting url and custom format so ...

grid.Column("FirstName"),
grid.Column("LastName", format: item => item.LastName),
grid.Column("LivesIn.City"),
grid.Column("LivesIn.Country")

...一切都很好。

... everything is ok

如何复杂类型?

grid.Column("FirstName"),
grid.Column("LastName"),
grid.Column("LivesIn.MyNonExistingField", format: item => item.LivesIn.City),
grid.Column("LivesIn.Country")

...哇...一切都确定..这是错误的孩子..栏目LivesIn.MyNonExistingField有适当的分类网址。

.... wow ... everything is ok .. that's kid of bug .. column "LivesIn.MyNonExistingField" has proper sorting url.

好吧......如果我们不希望暴露我们的域结构是什么。然后,我们需要结合过程中添加列名的列表

Ok ... What if we don't want to expose our domain structure. Then we need to add list of column names during binding

var grid = new WebGrid(persons, columnNames: new [] { "Foo" });
-- or --
grid.Bind(persons, columnNames: new [] { "Foo" });

grid.Column("Foo", format: item => item.FirstName),
grid.Column("LastName"),
grid.Column("LivesIn.MyNonExistingField", format: item => item.LivesIn.City),
grid.Column("LivesIn.Country")

..现在美孚列有适当的分类网址

.. now Foo column has proper sorting url

但仔细!还有另外一个错误。

But careful !!! There is another bug.

如果我们手工列名绑定,那么所有列,直到手动找到列,将被跳过补充。例如:

If we add manual column names to binding then all column will be skipped until manual column is found. Example :

var grid = new WebGrid(persons, columnNames: new [] { "Foo" });
-- or --
grid.Bind(persons, columnNames: new [] { "Foo" });

grid.Column("FirstName"),
grid.Column("Foo", format: item => item.LastName),
grid.Column("LivesIn.MyNonExistingField", format: item => item.LivesIn.City),
grid.Column("LivesIn.Country")

...排序网址列名字,将无法正确生成... sortDir = ASC所有的时间......来解决这个插件也有姓作为列名是这样的:

... sorting url for column "FirstName" will not be generated correctly ... sortDir=ASC all the time ...to fix this add also a "FirstName" as column name like this:

var grid = new WebGrid(persons, columnNames: new [] { "FirstName", "Foo" });
-- or --
grid.Bind(persons, columnNames: new [] { "FirstName", "Foo" });

grid.Column("FirstName"),
grid.Column("Foo", format: item => item.LastName),
grid.Column("LivesIn.MyNonExistingField", format: item => item.LivesIn.City),
grid.Column("LivesIn.Country")

@Kohen

@Kohen

在code取出COLUMNNAMES此处

Remove columnNames in your code here

var grid = new System.Web.Helpers.WebGrid(Model.SubscriptionList,  
          columnNames: new List<string>(){"Title"},  
          canPage:false);

...或添加有你所有的列名,如ID,ISP,等等。

... or add there all your column names like "ID", "ISP", etc

这篇关于asp.net MVC 3的WebGrid整理遗体?sortdir = ASC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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