ajax将对象传递给控制器

时间:2014-05-20 08:55:43

标签: c# asp.net-mvc

HomeController:

      [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;
    }

4 个答案:

答案 0 :(得分:2)

以下是代码中的错误

1.你的模特没有吸气剂和制定者

public class CategoryOrderModel
{
    public int DisplayOrder { get; set; }
    public int CategoryId { get; set; }
}

2.因为它是javascript,所以operationCollection是模型所以它不起作用而不是变量

  var CategoryOrderModel =
  {
      "DisplayOrder": "7",
      "CategoryId": "9"
  };

        $.ajax({
            url: '@Url.Action("SetCategoryOrder", "Home")',
            dataType: 'json',
            data: '{ categories : ' + JSON.stringify(CategoryOrderModel) + '}',
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                alert(data.msg);

            },
            error: function (data) {
                alert('error');
            }
        });

答案 1 :(得分:1)

我们知道MVC的工作原理是名称/值对,即强类型视图和模型绑定。

可能你可以这样做:

 $.ajax({
                    url: '@Url.Action("SetDefaultHomeCategoryOrder", "Home")',
                    dataType: 'json',
                    data: {'DisplayOrder' : 9 , 'CategoryId' : 1},
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        alert(data.msg);

                    },
                    error: function (data) {
                        alert('error');
                    }
                });

注意数据键/值data: {'DisplayOrder' : 9 , 'CategoryId' : 1},,这有点类似于在MVC中发布表单。

通常我们使用$('#formId').serialize()通过jquery / ajax将视图集合发送到控制器。

答案 2 :(得分:0)

您正在尝试将数据传递给控制器​​“SetDefaultHomeCategoryOrder”或“SetCategoryOrder” 如果Action名称是SetCategoryOrder,则键入

url:@Url.Action('SetCategoryOrder','Home')

答案 3 :(得分:-1)

您的方法与您调用SetDefaultHomeCategoryOrder的视图不匹配,但在控制器内部称为SetCategoryOrder