你如何在asp.net mvc中坚持查询字符串值?

问题描述 投票:12回答:4

在asp.net mvc中保存查询字符串值的好方法是什么?

如果我有一个网址:/ questions?page = 2&sort = newest&items = 50&showcomments = 1&search = abcd

在分页链接上,我想在所有链接中保留这些查询字符串值,以便在用户点击“下一页”时保持这些值(在这种情况下,页面值会改变,但其余部分将保持不变)

我可以想到两种方法:

  1. View中的Request.Querystring并将值添加到链接
  2. 使用ViewData将每个查询字符串值从Controller传递回View

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

asp.net-mvc query-string viewdata
4个回答
6
投票

我会在视图中处理QueryString(您的选项#1),而不是从控制器传递它。此方法使视图更加自包含,允许您将其转换为视图控件并在不同视图中重用它。

注意:直接在视图中访问QueryString似乎违反了分离模型和视图的设计原则,但实际上这些数据是与视图相关的导航问题,而不是模型的真正部分。


8
投票

我使用扩展方法:

public static string RouteLinkWithExtraValues(
        this HtmlHelper htmlHelper,
        string name,
        object values)
    {
        var routeValues = new RouteValueDictionary(htmlHelper.ViewContext.RouteData.Values);

        var extraValues = new RouteValueDictionary(values);
        foreach (var val in extraValues)
        {
            if (!routeValues.ContainsKey(val.Key))
                routeValues.Add(val.Key, val.Value);
            else
                routeValues[val.Key] = val.Value;
        }

        foreach (string key in htmlHelper.ViewContext.HttpContext.Request.Form)
        {
            routeValues[key] = htmlHelper.ViewContext.HttpContext.Request.Form[key];
        }

        foreach (string key in htmlHelper.ViewContext.HttpContext.Request.QueryString)
        {
            if (!routeValues.ContainsKey(key) && htmlHelper.ViewContext.HttpContext.Request.QueryString[key] != "")
                routeValues[key] = htmlHelper.ViewContext.HttpContext.Request.QueryString[key];
        }

        var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);

        return string.Format("<a href=\"{0}\">{1}</a>", urlHelper.RouteUrl(routeValues), name);
    }

1
投票

我只是将会话中的值保持在分页链接只需要的方式;

/问题?页= 2

/问题?页= 3

我不想使用QueryString的一个原因是因为我不希望用户看到我传递给程序的值。这使得他们很容易进入地址栏并开始将值更改为“看看会发生什么”。使用此代码,他们所能做的就是更改页码。


1
投票

以下是我在Asp.Net Core中的操作方法,首先将查询字符串参数分配给控制器中的ViewBags:

[HttpGet("/[controller]/[action]/{categoryId?}/{contractTypeId?}/{locationId?}")]
public IActionResult Index(Guid categoryId, int contractTypeId, Guid locationId)
{
    ViewBag.CategoryId = categoryId;
    ViewBag.ContractTypeId = contractTypeId;
    ViewBag.LocationId = locationId;

    ...
}

然后将值传递给您的链接,如下所示:

<a asp-action="Index" asp-controller="Jobs"
   asp-route-categoryId="@teachingCategory.Id"
   asp-route-contractTypeId="@ViewBag.ContractTypeId"
   asp-route-locationId="@ViewBag.LocationId">
   @teachingCategory.Description (@teachingCategory.Rank)
</a>

<a asp-action="Index" asp-controller="Jobs"
   asp-route-categoryId="@ViewBag.CategoryId"
   asp-route-contractTypeId="@typeOfEmployment.Id"
   asp-route-locationId="@ViewBag.LocationId">
   @typeOfEmployment.Name
</a>

<a asp-action="Index" asp-controller="Jobs"
   asp-route-categoryId="@ViewBag.CategoryId"
   asp-route-contractTypeId="@ViewBag.ContractTypeId"
   asp-route-locationId="@item.Id">
   @item.Id
</a>

请注意,每个链接都保留其自己的实际值,并通过我们传递给ViewBag的路径传递剩余的路由值。

© www.soinside.com 2019 - 2024. All rights reserved.