2013-04-17 84 views
0

我有一个需要视图模型的操作。未填充MVC视图模型阵列

[HttpPost] 
public JsonResult SearchAjax(JQueryDataTablesModel jQueryDataTablesModel, BloodSearchAjaxViewModel searchModel) 
{ 
... 

在该视图模型有一个数组

public ReadOnlyCollection<string> mDataProp_ { get; set; } 

当我调用动作我经由提琴手验证阵列数据被传递

enter image description here

然而,该阵列(以及viewmodel中的其他数组)都是null。

另外,如果我在视图模型中放入一个名为mDataProp_0的字段,它将被填充。

根据评论更新。以下是发布数据的视图中的代码。我正在使用jQueryDataTable。我认为这段代码并不重要,因为我确认数据在http请求中。

/* Initialize table */ 
     var oTable = $('#search-results-table').dataTableWithFilter({ 
       "sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>", 
       "sPaginationType": "bootstrap", 
       "bProcessing": true, 
       "bServerSide": true, 
       "sAjaxSource": 'SearchAjax', 
       "sServerMethod": "POST", 
       "aoColumns": [ 
        { "mDataProp": "BloodIdentificationNumber" }, 
        { "mDataProp": "Status" }, 
        { "mDataProp": "ExpirationDate" }, 
        { "mDataProp": "CompanyName" }, 
        { "mDataProp": "Location" }, 
        { "mDataProp": "City" } 
       ], 
       // Initialize our custom filtering buttons and the container that the inputs live in 
       filterOptions: { searchButton: "search-button", clearSearchButton: "clear-search-button", searchContainer: "search-block" } 
     }); 

任何想法?谢谢!

+0

你在使用ajax/jQuery调用方法吗?你能从视图中发布相关代码吗? – lopezbertoni

+0

你可以提供所有字段的完整ajax请求吗? –

回答

0

默认的模型联编程序非常挑剔。我认为你在这里看到的只是数据不符合活页夹所期望的约定。您的选项是1)将数据按摩到默认粘结剂所接受的数据或2)编写自定义模型粘结剂。以下是两种方法的一些链接。

ASP.NET MVC - Custom model binder able to process arrays http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

+0

当我第一次读到你的答案时,我想:“不,那不是。数据结构正确,我过去曾多次使用MVC处理数组。但是不,我错了:(我回去读了一篇旧文章,并意识到你是对的,数据的格式确实与我所见过的不同(以及MVC处理的盒子)。写了一个自定义模型绑定器,并且一切正常,谢谢! –

0

我找到了解决办法。研究这个项目,我发现你必须在Global.asax文件中包含一行。

protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 

     RegisterGlobalFilters(GlobalFilters.Filters); 
     RegisterRoutes(RouteTable.Routes); 

     // This line 
     ModelBinders.Binders.Add(typeof(JQueryDataTablesModel), new JQueryDataTablesModelBinder()); 
    } 

凡为参数如下行JQueryDataTablesModel使用中应包含在Global.asax,问题就解决了。

+0

请注意,您正在回答一年前的问题,并且请求者已经设置了一个已批准的答案,最好回答较新和未批准的问题。 – ndsmyter