2013-04-17 86 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