2016-05-12 37 views
0

如何使用3個參數和序列化表單調用我的控制器的操作方法?使用多個參數調用我的控制器,包括序列化表格

這是我有,但'SearchCriteria sc'參數爲空。

public JsonResult CreateDynamicCollection(int[] arrIds, string collectionName, int parentCollectionId, SearchCriteria sc) 
{ 
    // sc is null 
} 

public class SearchCriteria 
{ 

    public string City { get; set; } 

    public string PostalCode { get; set; } 

    // other fields here left out 
} 

var model = {}; 
 
model.arrIds = arrIds; 
 
model.parentCollectionId = parentId; 
 
model.collectionName = $createCollectionName.val(); 
 
var form = $('#formAdvSearch')[0]; 
 
model.form = $(form).serialize(); 
 

 
$.post(controller, model, function(response) { 
 
    if (response.error == false) { 
 
     //do some stuff 
 
    } 
 
    }) 
 
    .fail(function() { 
 

 
    }) 
 
    .always(function() { 
 

 
    });

+0

你正在做什麼都不行。您無法發送帶有序列化屬性的模型。 –

+0

它的作品,如果這就是我沒有其他參數發送! – user1186050

+0

如果你打算使用'.serialize()',那麼你需要使用'$ .param()'方法添加其他值。參考[這個答案](http://stackoverflow.com/questions/32353093/mvc-jquery-ajax-post-returns-null/32353268#32353268)爲例。 –

回答

0

這是我做什麼,

我在一個更復雜的serialzed數據結構中傳遞。

var form = $('#formAdvSearch')[0]; 
 
var serializedForm = $(form).serializeArray(); 
 
serializedForm.push({ 
 
    name: 'arrIds', 
 
    value: arrIds 
 
}); 
 
serializedForm.push({ 
 
    name: 'parentCollectionId', 
 
    value: parentId 
 
}); 
 
serializedForm.push({ 
 
    name: 'collectionName', 
 
    value: $createCollectionName.val() 
 
}); 
 
model = serializedForm;

然後我做了我新的對象接受模型繼承舊的對象和新的項目被安置在新的OBJ

相關問題