简体   繁体   English

带有jQuery数据表的C#MVC:从GET检索变量

[英]C# MVC with jQuery Datatables: Retrieving variables from GET

This is the query string that Datatables.net sends to my MVC action: 这是Datatables.net发送到我的MVC操作的查询字符串:

?sEcho=8&iColumns=6&sColumns=&iDisplayStart=0&iDisplayLength=10&mDataProp_0=HomeCountry&mDataProp_1=HostCountry&mDataProp_2=YearOneRate&mDataProp_3=YearOtherRate&mDataProp_4=RateType&mDataProp_5=Controls&sSearch=&bRegex=false&sSearch_0=&bRegex_0=false&bSearchable_0=true&sSearch_1=&bRegex_1=false&bSearchable_1=true&sSearch_2=&bRegex_2=false&bSearchable_2=true&sSearch_3=&bRegex_3=false&bSearchable_3=true&sSearch_4=&bRegex_4=false&bSearchable_4=true&sSearch_5=&bRegex_5=false&bSearchable_5=true&iSortCol_0=1&sSortDir_0=asc&iSortingCols=1&bSortable_0=true&bSortable_1=true&bSortable_2=true&bSortable_3=true&bSortable_4=true&bSortable_5=false&_=1391446190711 ?sEcho = 8&iColumns = 6&sColumns =&iDisplayStart = 0&iDisplayLength = 10&mDataProp_0 = HomeCountry&mDataProp_1 = HostCountry&mDataProp_2 = YearOneRate&mDataProp_3 = YearOtherRate&mDataProp_4 = RateType&mDataProp_5 =控制与SSEARCH =&bRegex =假sSearch_0 = bRegex_0 =假bSearchable_0 =真sSearch_1 =&bRegex_1 =假bSearchable_1 =真sSearch_2 =&bRegex_2 =假bSearchable_2 =真sSearch_3 =&bRegex_3 =假bSearchable_3& = true&sSearch_4 =&bRegex_4 = false&bSearchable_4 = true&sSearch_5 =&bRegex_5 = false&bSearchable_5 = true&iSortCol_0 = 1&sSortDir_0 = asc&iSortingCols = 1&bSortable_0 = true&bSortable_1 = true&bSortable = 4&711

This is my controller action header in MVC: 这是我在MVC中的控制器操作标头:

public JsonResult GetData(int sEcho, int iDisplayStart, int iDisplayLength, string sSearch)

My question is: How do I get variables such as these (?): 我的问题是:我如何获得这些变量(?):

bSortable_0=true&bSortable_1=true&bSortable_2=true&bSortable_3=true&bSortable_4=true&bSortable_5=false bSortable_0 = true&bSortable_1 = true&bSortable_2 = true&bSortable_3 = true&bSortable_4 = true&bSortable_5 = false

Notice how the number after bSortable_ could be anything from 0 to 5 or more. 请注意, bSortable_之后的数字如何可以是0到5或更大的值。

You can get all the query string parameters from the Request.QueryString collection, including the ones you didn't make method parameters. 您可以从Request.QueryString集合中获取所有查询字符串参数,包括您没有设置方法参数的参数。 You can find the individual keys in Request.QueryString.Keys . 您可以在Request.QueryString.Keys找到各个键。 Using that, you could loop through the collection and grab each key/value pair. 使用它,您可以遍历集合并获取每个键/值对。

To parse querystring - 解析查询字符串-

NameValueCollection queryCollection = HttpUtility.ParseQueryString(Request.Url.Query);
var items = queryCollection
                 .AllKeys
                 .SelectMany(queryCollection.GetValues, (k, v) => new { key = k, value = v })
                 .Where(p => p.key.Contains("bSortable"))
                 .ToList();

And the output - 和输出-

在此处输入图片说明

There are a couple other ways. 还有其他几种方法。 You could do: 您可以这样做:

string bSearchable0 = Request["bSearchable_0"];

Or you could make a class and have your action method take it as a parameter: 或者,您可以创建一个类,并让您的action方法将其作为参数:

public class jQueryDataTableParam
{
    public int sEcho { get; set; }
    public int iDisplayStart { get; set; } 
    public int iDisplayLength { get; set; } 
    public string sSearch { get; set; }
    public bool bSearchable_1 { get; set; }
    public bool bSortable_1 { get; set; }
    //....
}

Using the above make your action method look more like 使用上面的方法使您的操作方法看起来更像

public JsonResult GetData(jQueryDataTableParam param)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM