【发布时间】:2014-05-20 18:06:42
【问题描述】:
主控制器:
[HttpPost]
public JsonResult SetDefaultHomeCategoryOrder(CategoryOrderModel categories)
{
return Json(new { msg = "ok" });
}
public class CategoryOrderModel
{
public int DisplayOrder;
public int CategoryId;
}
View:
var operationCollection = new CategoryOrderModel();
$.ajax({
url: '@Url.Action("SetDefaultHomeCategoryOrder", "Home")',
dataType: 'json',
data: JSON.stringify(operationCollection),
type: 'POST',
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data.msg);
},
error: function (data) {
alert('error');
}
});
控制器永远得不到正确的参数??
更新:我更改了代码以接受集合
[HttpPost]
public JsonResult SetDefaultHomeCategoryOrder(List<CategoryOrderModel> categories)
{
return Json(new { msg = 'ok' });
}
查看:
var collection = [];
var col1= new CategoryOrderModel(1,2);
collection.push(col1);
var col2= new CategoryOrderModel(2,5);
collection.push(col2);
$.ajax({
url: '/Home/SetDefaultHomeCategoryOrder/',
dataType: 'json',
data: '{ categories : ' + JSON.stringify(collection) + '}',
type: 'POST',
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data.msg);
},
error: function (data) {
alert('error');
}
});
function CategoryOrderModel(displayOrder, categoryId) {
var self = this;
self.DisplayOrder = displayOrder;
self.CategoryId = categoryId;
}
【问题讨论】:
-
SetDefaultHomeCategoryOrder- 您的问题中没有证据表明此操作甚至存在。 -
我认为您需要用您的操作方法 SetCategoryOrder 替换“SetDefaultHomeCategoryOrder”
-
您是否尝试过使用 firebug 或其他调试器查看控制台输出?
-
ReferenceError: CategoryOrderModel 未定义 var operationCollection = new CategoryOrderModel(); >> 来自萤火虫控制台
-
因此您在客户端使用服务器端 C# 对象。哪个不起作用尝试@RollerCosta 解决方案,应该可以。
标签: c# asp.net-mvc