使用ASP.NET MVC 2时更好的方式来留住过滤选项指引 [英] Guidance on a better way to retain filtering options when using ASP.NET MVC 2

查看:180
本文介绍了使用ASP.NET MVC 2时更好的方式来留住过滤选项指引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC 2应用程序,它在某种程度上允许用户过滤数据和查看在jqGrid的数据。

I have an ASP.NET MVC 2 application which in part allows a user to filter data and view that data in a JQGrid.

目前这种由一个控制器,它初始化我的滤镜模式,并配置我多么希望要显示我的网格。该信息由一个视图,并显示过滤器和电网外壳的局部视图中。我使用编辑器模板来显示我的过滤器。所述的jqGrid利用一个JsonResult控制器操作(GET)的检索滤波器的结果(通过加入由电网提供的寻呼的 - 仅数据的单个页面由GET请求返回所使用的栅格的URI。将请求数据包含了过滤器模型作为RouteValue - 目前包含一个字符串再过滤器的当前状态的presentation自定义IModelBinder用于这种重新presentation转换回过滤器模型的实例。类。

Currently this consists of a controller which initialises my filter model and configures how I wish my grid to be displayed. This information is used by a view and a partial view to display the filter and the grid shell. I use an editor template to display my filter. The JQGrid makes use of a JsonResult controller action (GET) to retrieve the results of the filter (with the addition of the paging offered by the grid - only a single page of data is returned by the GET request. The Uri used by the grid to request data contains the filter model as a RouteValue - and currently contains a string representation of the current state of the filter. A custom IModelBinder is used to convert this representation back into an instance of the filter model class.

用户可以更换过滤器和preSS一个提交按钮来得到不同的结果 - 这是然后用(HttpPost)的ViewResult行动这需要过滤模型拾起 - 通过进一步的模型绑定重组,并导致电网壳牌进行更新。

The user can change the filter and press a submit button to get different results - this is then picked up by an (HttpPost) ViewResult action which takes the filter model - reconstituted by a further model binder and causes the grid shell to be updated.

所以我有:

FilterModel
重新presents用户的期望的滤波特性

FilterModel Represents the user's desired filtering characteristics

FilterModelEditorTemplateSubmissionBinder:DefaultModelBinder - 用来从一个用户改变他们的滤波特性到相应的FilterModel实例提供的请求信息转换

FilterModelEditorTemplateSubmissionBinder : DefaultModelBinder - used to convert the request information supplied from a user changing their filtering characteristics into the appropriate FilterModel instance.

FilterModelStringRe presentationBinder:IModelBinder - 用来连接codeD过滤器从jqGrid的转换,所以正确的请求时它最终执行查询服务的GET的数据请求和返回的相关数据。

FilterModelStringRepresentationBinder : IModelBinder - used to convert the encoded filter from the JQGrid GET request for data so the correct request is made of the service which is ultimately performing the query and returning the relevant data.

的ViewResult指数() - 构建一个默认的过滤器,配置网格规范并返回视图渲染过滤器的编辑模板,网格外壳

ViewResult Index() - constructs a default filter, configures the grid specification and returns the view to render the filter's editor template, and the grid shell.

[HttpPost]的ViewResult滤波器(FilterModel过滤器) - 需要新的滤波特性并返回相同的视图索引()。用途FilterModelEditorTemplateSubmissionBinder到过滤器模型绑定。

[HttpPost]ViewResult Filter(FilterModel filter) - takes the new filter characteristics and returns the same view as Index(). Uses FilterModelEditorTemplateSubmissionBinder to bind the filter model.

JsonResult的GetData(FilterModel过滤器,串SIDX,串SORD,诠释页,诠释行) - 为了检索数据从jqGrid的调用。使用FilterModelStringRe presentationBinder到过滤器模型绑定。

JsonResult GetData(FilterModel filter, string sidx, string sord, int page, int rows) - called from the JQGrid in order to retrieve the data. Uses FilterModelStringRepresentationBinder to bind the filter model.

作为一个并发症,我的过滤器模型包含一个选项,选择项目的集合的单个值。这个系列是从服务请求检索和我不想让查询此数据,每次我显示过滤器,目前我得到它,如果属性为null,然后包括隐藏在编辑器模板和编码的选项字符串重新presentation。这些选项然后由相关模型粘合剂重组

As a complication, my filter model contains a option to select a single value from a collection of items. This collection is retrieved from a service request and I don't want to keep querying for this data everytime I show the filter, currently I get it if the property is null, and then include the options hidden in the editor template and encoding in the string representation. These options are then reconstituted by the relevant model binder.

虽然这种方法的工作原理我不禁觉得我不必为了维护我的过滤器和选项包括基本视图状态重塑。由于我是新来的ASP.NET MVC,但我很高兴与传统的ASP和ASP.NET Web窗体我想我会抛出此那里征求意见和指导,发现它与MVC模式更紧密地配合的方法。

Although this approach works I can't help but feel that I am having to basically reinvent viewstate in order to maintain my filter and the included options. As I am new to ASP.NET MVC but am very happy with classic ASP and ASP.NET Web Forms I thought I'd throw this out there for comment and guidance as to find a way which more closely fits with the MVC pattern.

推荐答案

在我看来,在最好的方式来划分的一些行动提供其从其他控制器操作的jqGrid的纯数据。这样的jqGrid导向的行为可以有原型,如:

It seems to me that the best way in to divide some actions which provide pure data for the jqGrid from other controller action. Such jqGrid-oriented actions can have prototype like:

JsonResult GetData(string filter, string sidx, string sord, int page, int rows)

我个人preFER实现此部分如WCF服务,并有此WCF服务作为相同ASP.NET站点的一部分。在一般情况它更品味的问题,取决于你的其他项目的要求。

I personally prefer to implement this part as WCF service and to have this WCF service as a part of the same ASP.NET site. In general it's much more the matter of taste and depends on your other project requirements.

你这部分ASP.NET站点可以实现用户的认证,你需要,可以与单元测试酷似你的控制器的其他动作进行测试。

This part of you ASP.NET site could implement users authentication which you need and can be tested with unit tests exactly like other actions of your controllers.

在ASP.NET MVC网站的观点可以有空数据作为jqGrids,并有唯一正确的网址,并可能生成HTML $ C $,c取决于在该网站的用户权限。每个页面都会填写jqGrids的数据相对于对应请求到服务器(请求到相应的的GetData 操作)的。

The views of the ASP.NET MVC site can have empty data for jqGrids, and have only correct URLs and probably generate the HTML code depends on the users permission in the site. Every page will fill the data of jqGrids with respect of the corresponds requests to the server (request to the corresponding GetData action).

您可以使用HTTP GET为最佳的数据高速缓存中的数据。数据的高速缓存是一个单独的讨论的主题。如果你这样做,你应该使用 prmNames:{ND:空} 在jqGrid的定义,除去独特的第二参数每默认添加到每个GET请求的时间戳。为了对服务器端例如,您可以添加在服务器响应都的Cache-Control设置为最大年龄= 0总部设在响应中返回的数据计算出的值的ETag头。您应该测试来自客户端的请求是否有如果 - 无 - 匹配 HTTP与头值的ETag coresponds缓存在客户端上的数据。那么你应该核实是否在服务器上的当前数据(在数据库中)被改变,如果有没有改变,生成一个体(集响应燮pressEntityBody 为true),并返回304未修改状态code(的HTTPStatus code.NotModified )而不是默认的200 OK。更详细的解释是更长时间。

You can use HTTP GET for the data for the best data caching. The caching of data is the subject of a separate discussion. If you do this, you should use prmNames: { nd:null } in the definition of jqGrid to remove unique nd parameter with the timestamp added per default to every GET request. To have full control of the data caching on the server side you can for example add in HTTP headers of the server responses both "Cache-Control" set to "max-age=0" and "ETag" header with the value calculated based of the data returned in the response. You should test whether the request from the client has "If-None-Match" HTTP header with the value of "ETag" coresponds the data cached on the client. Then you should verify whether the current data on the server (in the database) are changed and, if there are not changed, generate a response with an empty body (set SuppressEntityBody to true) and return "304 Not Modified" status code (HttpStatusCode.NotModified) instead of default "200 OK". A more detail explanation is much more longer.

如果您不希望您优化网站HTTP GET你可以使用HTTP POST或者不使用 prmNames jqGrids数据缓存:{ND:空} 参数。

If you don't want optimize you site for caching of HTTP GET data for jqGrids you can either use HTTP POST or don't use prmNames: { nd:null } parameter.

里面的code JsonResult的GetData(字符串过滤器,串SIDX,串SORD,诠释页,诠释行)不是很短的原因。你应该从过滤器字符串deserialise JSON数据,然后构建请求到数据模型依赖的数据访问您使用(LINQ到SQL,实体模型或方法的SqlCommand SqlDataReader的)。因为你这部分已经实现了它没有意义,讨论这一部分。

The code inside of JsonResult GetData(string filter, string sidx, string sord, int page, int rows) is not very short of cause. You should deserialise JSON data from the filter string and then construct the request to the data model depends on the method of the data access which you use (LINQ to SQL, Entity Model or SqlCommand with SqlDataReader). Because you have this part already implemented it has no sense to discuss this part.

也许我的建议的主要部分是(只具有<$ C $的这为所有jqGrids数据和MVC的观点是空的​​数据使用控制器操作的明确分离的使用C>&LT;表ID =清单&GT;&LT; /表&gt;&LT; D​​IV ID =寻呼机&GT;&LT; / DIV&GT; )。你也应该与拥有相对长的code为它来自的jqGrid和您的数据模型中相应的请求高级搜索功能生成或过滤器分析毫无疑问的。只是实现它一次。在我的实施code中也比较复杂,但它已经写入一次时,它的工作原理,它可以用于所有新jqGrids

Probably the main part of my suggestion is the usage of clear separation of controller actions which provide the data for all your jqGrids and the usage of MVC views with empty data (having only <table id="list"></table><div id="pager"></div>). You should also has no doubt with having a relative long code for analyzing of filters which come from the Advance Searching feature of the jqGrid and generating or the corresponding requests to your data model. Just implement it one time. In my implementation the code in also relatively complex, but it is already written one time, it works and it can be used for all new jqGrids.

这篇关于使用ASP.NET MVC 2时更好的方式来留住过滤选项指引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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