I'll try to make this as concise as possible.
我会尝试尽可能简洁。
- Webpage contains a table that allows for filtering and sorting
- Changes to filtering and sorting should be reflected in the URL so the user can bookmark or share filtered views.
网页包含一个允许过滤和排序的表
过滤和排序的更改应反映在URL中,以便用户可以书签或共享过滤的视图。
The question is: What is an effective convention of allowing all of the sort and filter syntax to be part of the URL and easily interpret/use it on the server without having to write a bunch of custom code that interprets it?
问题是:什么是允许所有排序和过滤器语法成为URL的一部分并在服务器上轻松解释/使用它而无需编写一堆解释它的自定义代码的有效约定?
I've been doing some research and I came across the OData URI conventions and I like the way they do things. http://www.odata.org/developers/protocols/uri-conventions
我一直在做一些研究,我遇到了OData URI约定,我喜欢他们做事的方式。 http://www.odata.org/developers/protocols/uri-conventions
More research shows the the MVC 4 Web API allows for use of that convention by returning an IQueryable. This looks fantastic except for one part... I'm not implementing a RESTful API at this point and that's all it seems to work with. So how can I use something like OData and still return a View or PartialView? Is there something that will parse the OData URI convention into a C# object?
更多研究表明,MVC 4 Web API允许通过返回IQueryable来使用该约定。除了一部分之外,这看起来很棒......我现在还没有实现RESTful API,而这似乎与它有关。那么我怎样才能使用像OData这样的东西并仍然返回View或PartialView?是否有能够将OData URI约定解析为C#对象的东西?
If anyone has any insights into this problem or suggestions, I'm all ears.
如果有人对这个问题或建议有任何见解,我会全力以赴。
2 个解决方案
#1
0
As for the url convention part of your question, I think you have answered your own question with OData. As for getting this data into a C# object I would use the following approach:
至于问题的url约定部分,我想你已经用OData回答了你自己的问题。至于将此数据转换为C#对象,我将使用以下方法:
Use an action filter to interperet the url parameters and parse them into a c# object. In your action filter add the url parameters to the route data and the c# object will be available in your action.
使用动作过滤器来插入url参数并将它们解析为c#对象。在您的操作过滤器中,将url参数添加到路径数据中,c#对象将在您的操作中可用。
ASP.NET MVC Pass object from Custom Action Filter to Action
从自定义操作过滤器到操作的ASP.NET MVC Pass对象
Take a look at the Telerik MVC grid, they use a GridAction action filter that does pretty much what you are asking.
看一下Telerik MVC网格,他们使用GridAction动作过滤器来完成你所要求的几乎所有。
#2
0
I would look at custom model binding. A good overview can be found here: http://weblogs.asp.net/nmarun/archive/2010/02/25/asp-net-mvc-model-binding.aspx
我会看看自定义模型绑定。可以在这里找到一个很好的概述:http://weblogs.asp.net/nmarun/archive/2010/02/25/asp-net-mvc-model-binding.aspx
It's typically used for POST requests with forms but there's no reason why you can't use it for GET requests too.
它通常用于带有表单的POST请求,但没有理由不能将它用于GET请求。
Basically, your approach should be to:
基本上,您的方法应该是:
-
Create a new Model class with your filter/sorting parameters as properties:
使用过滤器/排序参数作为属性创建一个新的Model类:
public class TableParameters { public string TableFilter { get; set; } }
-
In your Controller's Action, add the model as a parameter
在Controller的Action中,将模型添加为参数
public ActionResult TableAction(TableParameters parameters) { /* Action logic */ }
-
Set your parameters in the URL by saying:
通过以下方式在URL中设置参数:
/Controller/TableAction?TableFilter=[your-filter-string]
The parameters object in your action will have the property populated with the value from the query string.
操作中的参数对象将使用查询字符串中的值填充该属性。
#1
0
As for the url convention part of your question, I think you have answered your own question with OData. As for getting this data into a C# object I would use the following approach:
至于问题的url约定部分,我想你已经用OData回答了你自己的问题。至于将此数据转换为C#对象,我将使用以下方法:
Use an action filter to interperet the url parameters and parse them into a c# object. In your action filter add the url parameters to the route data and the c# object will be available in your action.
使用动作过滤器来插入url参数并将它们解析为c#对象。在您的操作过滤器中,将url参数添加到路径数据中,c#对象将在您的操作中可用。
ASP.NET MVC Pass object from Custom Action Filter to Action
从自定义操作过滤器到操作的ASP.NET MVC Pass对象
Take a look at the Telerik MVC grid, they use a GridAction action filter that does pretty much what you are asking.
看一下Telerik MVC网格,他们使用GridAction动作过滤器来完成你所要求的几乎所有。
#2
0
I would look at custom model binding. A good overview can be found here: http://weblogs.asp.net/nmarun/archive/2010/02/25/asp-net-mvc-model-binding.aspx
我会看看自定义模型绑定。可以在这里找到一个很好的概述:http://weblogs.asp.net/nmarun/archive/2010/02/25/asp-net-mvc-model-binding.aspx
It's typically used for POST requests with forms but there's no reason why you can't use it for GET requests too.
它通常用于带有表单的POST请求,但没有理由不能将它用于GET请求。
Basically, your approach should be to:
基本上,您的方法应该是:
-
Create a new Model class with your filter/sorting parameters as properties:
使用过滤器/排序参数作为属性创建一个新的Model类:
public class TableParameters { public string TableFilter { get; set; } }
-
In your Controller's Action, add the model as a parameter
在Controller的Action中,将模型添加为参数
public ActionResult TableAction(TableParameters parameters) { /* Action logic */ }
-
Set your parameters in the URL by saying:
通过以下方式在URL中设置参数:
/Controller/TableAction?TableFilter=[your-filter-string]
The parameters object in your action will have the property populated with the value from the query string.
操作中的参数对象将使用查询字符串中的值填充该属性。