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

前端 未结 4 1775
独厮守ぢ
独厮守ぢ 2021-01-01 21:24

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

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

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-01 21:57

    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:

    
       @teachingCategory.Description (@teachingCategory.Rank)
    
    
    
       @typeOfEmployment.Name
    
    
    
       @item.Id
    
    

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

提交回复
热议问题