Код JQuery:
//This passes NULL for "CategoryID", "CategoryName", "ProductID", "ProductName" $("#btnPost").click(function () { var CategoryModel = { CategoryID: 1, CategoryName: "Beverage" }; var ProductModel = { ProductID: 1, ProductName: "Chai" }; var data1 = {}; data1["cat"] = CategoryModel; data1["prd"] = ProductModel; var jsonData = JSON.stringify(data1); $.ajax({ url: url + '/Complex/ModelTwo', //This works but property values are null type: 'post', dataType: 'json', data: { "cat": CategoryModel, "prd": ProductModel }, //jsonData, cache: false, success: function (result) { alert(result); }, error: function (xhr, ajaxOptions, thrownError) { alert(thrownError); } }); });
Код MVC (С#):
public class ComplexController : Controller
{
public string ModelOne(Category cat)
{
return "this took single model...";
}
public string ModelTwo(Category cat, Product prd)
{
return "this took multiple model...";
}
}
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
}
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
}
Теперь проблема заключается в том, что я не мог заставить ее работать, передав "MethodModel", "ProductModel" в метод действия "ModelTwo" . Сообщение JQuery правильно идентифицирует метод действия "ModelTwo" , но значения свойств "cat", "prd" равны null. "CategoryID", "CategoryName", "ProductID", "ProductName" все являются нулевыми, несмотря на то, что они ударили этот метод.
//THIS ONE WORKS FINE... $("#btnPost").click(function () { var CategoryModel = { CategoryID: 1, CategoryName: "Beverage" }; var ProductModel = { ProductID: 1, ProductName: "Chai" }; var data1 = {}; data1["cat"] = CategoryModel; data1["prd"] = ProductModel; var jsonData = JSON.stringify(data1); $.ajax({ url: url + '/Complex/ModelOne', //This works type: 'post', dataType: 'json', data: CategoryModel, cache: false, success: function (result) { alert(result); }, error: function (xhr, ajaxOptions, thrownError) { alert(thrownError); } }); });
Итак, что случилось с моим первым вызовом JQuery к методу действий "ModelTwo" ?
Я потратил много времени на это, но не уверен, что происходит. Здесь нет проблем с маршрутизацией, потому что я могу приземлиться на правильный метод действия...
Любая помощь будет принята с благодарностью.
Спасибо!