你如何坚持asp.net的MVC查询字符串值? [英] How do you persist querystring values in asp.net mvc?

查看:130
本文介绍了你如何坚持asp.net的MVC查询字符串值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是asp.net的MVC坚持查询字符串值的好办法?

What is a good way to persist querystring values in asp.net mvc?

如果我有一个网址:
?/问题网页= 2及排序=最新和放大器;项目= 50安培; showcomments = 1&安培;搜索= ABCD

If I have a url: /questions?page=2&sort=newest&items=50&showcomments=1&search=abcd

在分页链接我要保持所有的链接的查询字符串值,使他们坚持的时候,下页,例如用户点击(在这种情况下,页面的价值会改变,但其余的将保持不变)

On paging links I want to keep those querystring values in all the links so they persist when the user clicks on the "next page" for example (in this case the page value would change, but the rest would stay the same)

我能想到的2种方法来做到这一点:

I can think of 2 ways to do this:


  1. 的Request.QueryString在视图和值添加到链接

  2. 使用ViewData的传递从控制器的每个查询字符串值回视图

时比另一个好?那些是唯一的选择还是有更好的方式来做到这一点?

Is one better than the other? Are those the only options or is there a better way to do this?

推荐答案

我将处理在视图中的查询字符串(您的选择#1),而不是把它传到控制器。这种方法使得查看更多自成体系,让你把它转换成一个视图控件,并重新使用它在不同的看法。

I would process the QueryString in the view (your option #1), instead of passing it in from the controller. This approach makes the view more self-contained, allowing you to convert it into a view control and re-use it across different views.

请注意:直接在视图访问查询字符串可能看起来像一个违反分离模型和视图的设计原则,但在现实中,这个数据是关系到视图导航的关注,而不是模型的真正组成部分

Note: Accessing the QueryString directly in the view may seem like a violation of the design principle of separating the Model and View, but in reality this data is a navigational concern which is related to the view, not really part of the model.

这篇关于你如何坚持asp.net的MVC查询字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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