【问题标题】:angularjs post data to mvc controllerangularjs 将数据发布到 mvc 控制器
【发布时间】:2014-12-18 09:03:33
【问题描述】:

我有使用 angularjs 的简单代码,

 $http.post(postToCreateURL, addingProducts, {
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
    })
        .success(function () {
        })
        .error(function () {
        });

我想将此数据绑定到这个 Mvc 控制器

[HttpPost]
    public JsonResult Create(ProductModel model)
    {
        NorthwindEntities db = new NorthwindEntities();

        var products = db.Products.Select(s => new ProductModel
        {
            ProductName = s.ProductName,
            SupplierID = s.SupplierID,
            CategoryID = s.CategoryID,
            QuantityPerUnit = s.QuantityPerUnit,
            UnitPrice = s.UnitPrice,
            UnitsInStock = s.UnitsInStock,
            UnitsOnOrder = s.UnitsOnOrder,
            ReorderLevel = s.ReorderLevel,
            Discontinued = s.Discontinued

        }).Take(5).ToList();

        return Json(products, JsonRequestBehavior.AllowGet);
    }

在这种情况下,我得到的是空值。提前致谢!

【问题讨论】:

  • 标题:{'Content-Type': undefined} ?
  • 如果我没有使用它,它在发送请求时返回错误并且无法发送
  • 如果你使用application/json?
  • 你能告诉你如何在你的 mvc 控制器中使用 angularjs 控制器发送的 json 数据

标签: asp.net-mvc angularjs post


【解决方案1】:

试试这个:

     var request = {
        ProductName: 'aa',
        SupplierID: 'aa',
        CategoryID: 'aa',

        ...

        Discontinued: 'zzz'
    } 




    $http.post(" URL IN HERE ", JSON.stringify(request), {
        headers: {
            'Content-Type': 'application/json'
        }
    })

【讨论】:

    猜你喜欢
    • 2018-12-01
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    相关资源
    最近更新 更多