How do you persist querystring values in asp.net mvc?

廉价感情. 提交于 2019-11-30 17:45:36

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.

i use a extension method for that:

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);
    }

I would just keep the values in the Session that way the paging links only need to have;

/questions?page=2

/questions?page=3

The one reason why I would not us the QueryString is because I don't want the user to see the values that I am passing to the program. It makes it way too easy for them to go into the address bar and start changing the values to 'see what happens'. With this code all they could do is change the page number.

Here's how I done it in Asp.Net Core, first assign the query string parameters to ViewBags in your controller:

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

    ...
}

Then pass the values to your links like so:

<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>

Note that every link keep its own actual value and pass the rest of the route values through what we passed to ViewBag.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!