[英]how to access Javascript multidimensional array in MVC controller
我必須傳遞這樣的過濾器數組:
腳本代碼:
return { CustomFilter: options.filter.filters };
++++++++++++++++++++++++++++++++++++++++++++++++
來自Firebug:
CustomFilter[0][field] ItemName
CustomFilter[0][operator] startswith
CustomFilter[0][value] testing Value
CustomFilter[1][field] BrandName
CustomFilter[1][operator] startswith
CustomFilter[1][value] testing Value 1
發布的值是:
但我無法在控制器方面收到這些信息。
我試過這樣的:
public ActionResult ReadOperation( string[][] CustomFilter)
也像這樣:
public ActionResult ReadOperation( Filter[] CustomFilter)
public class Filter
{
public string field { get; set; }
public string @operator { get; set; }
public string value { get; set; }
}
但沒有奏效。 請建議。
謝謝。
使用Json反序列化找到解決方案
腳本代碼更改為:
return { CustomFilter: JSON.stringify(CustomFilter) };
控制器代碼更改為:
using Newtonsoft.Json;
public ActionResult ReadOperation(MyViewModel model)
{
var filters = JsonConvert.DeserializeObject(model.CustomFilter, typeof(CustomFilter[]));
}
public class MyViewModel
{
public string Filter { get; set; }
public string group { get; set; }
public int page { get; set; }
public int pageSize { get; set; }
public int sort { get; set; }
}
public class CustomFilter
{
public string field { get; set; }
public string @operator { get; set; }
public string value { get; set; }
}
控制器中的結果視圖:
它看起來像模型結構的錯誤。
public class MyViewModel
{
public Filter[] CustomFilter { get; set; }
public string Filter { get; set; }
public string Group { get; set; }
public int Page { get; set; }
public int PageSize { get; set; }
public int Sort { get; set; }
}
嘗試將此類型用於模型綁定。
public ActionResult ReadOperation(MyViewModel model)
在帖子中,嘗試發送數據:
CustomFilter[0].Field ItemName
CustomFilter[0].Operator startswith
CustomFilter[0].Value testing Value
CustomFilter[1].Field BrandName
CustomFilter[1].Operator startswith
CustomFilter[1].Value testing Value 1
在控制器:
public ActionResult ReadOperation(Filter[] CustomFilter)
將Filter
類定義為:
public class Filter
{
public string Field { set; get; }
public string Operator { set; get; }
public string Value { set; get; }
}
(小心大寫字母)。
或者如果你想使用模型方法,正如Ufuk建議的那樣,並且具有相同的Filter
類:
模型:
public class MyViewModel { public Filter[] CustomerFilter { get; set; } public string Filter { get; set; } public string Group { get; set; } public int Page { get; set; } public int PageSize { get; set; } public int Sort { get; set; } }
POST中的參數:
CustomFilter[0].Field ItemName CustomFilter[0].Operator startswith CustomFilter[0].Value testing Value CustomFilter[1].Field BrandName CustomFilter[1].Operator startswith CustomFilter[1].Value testing Value 1 Filter ItemName~startswith~'12'~and~BrandName~startswith~'123' Group Page 1 PageSize 15 Sort
調節器
public ActionResult ReadOperation(MyViewModel model)
請看這個鏈接: http : //haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/
如果您使用的是ASP.NET MVC 4,並且無法更改參數名稱,則可以通過以下方式定義自定義模型:
public class MyViewModel
{
public Dictionary<string,string>[] CustomerFilter { get; set; }
public string filter { get; set; }
public string group { get; set; }
public int page { get; set; }
public int pageSize { get; set; }
public int sort { get; set; }
}
然后,在控制器:
public ActionResult ReadOperation(MyViewModel model){ ... }
似乎網格生成的參數中使用的符號用於字典 。 但是,沒有嘗試過字典集。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.