【问题标题】:Web API Model properties are nullWeb API 模型属性为空
【发布时间】:2012-12-14 14:50:12
【问题描述】:

我的控制器能够创建模型对象,但所有与模型相关的属性都分配给空值

环境:VS 2010,ASP.NET MVC RC 最新,jQuery 1.7.1

以下是 Web API 控制器代码

public class Customer
{
    public string Name { get; set; }
    public string City { get; set; }
}
public class UserController : ApiController
{
    public Customer Post(Customer user)
    {
        return user;
    }
}

下面是ajax调用代码

$.ajax('/api/user',
{
  ContentType: "application/x-www-form-urlencoded; charset=UTF-8",
  dataType: 'json',
  type: 'POST',
  data: JSON.stringify({ "Name": "Scott", "City": "SC" })
});

Controller 确实创建了模型“Customer”对象,但“Name”和“City”属性均为空。

这里有什么问题?

我在这个网站上阅读了很多类似的问题,但找不到解决方案。

【问题讨论】:

  • 由于类名是Customer,我想说您还需要将传入的数据封装在Customer 属性中。 {"Customer" : { "Name": "Scott", "City": "SC" }}.
  • 感谢您的快速回复。我认为我们不需要传递类名。任何方式都试过了,我也有同样的问题。我也试过 {"user" : { "Name": "Scott", "City": "SC" }} 因为 user 是参数名称。
  • 经过几次尝试,我已将 ajax 代码修改为 $.ajax('/api/user', { ContentType: "application/x-www-form-urlencoded; charset=UTF-8" ,数据类型:'json',类型:'POST',数据:{“名称”:“斯科特”,“城市”:“SC”}});我删除了 JSON.stringify 并且它起作用了。
  • 把它写下来作为答案 - 不需要JSON.stringify...
  • 问题在于您试图发布嵌入在表单 URL 编码数据中的 JSON 字符串。您应该选择其中一个。要么发送原始 JSON 并使用“application/json”媒体类型,要么使用“application/x-www-form-urlencoded”发送表单 URL 编码数据。请参阅stackoverflow.com/questions/5570747/jquery-posting-json 了解犯了同样错误的人。

标签: asp.net asp.net-web-api


【解决方案1】:

This blog 在这里很好地说明了模型绑定在 ASP.NET Web 项目和 ASP.NET Web API 项目中的不同之处。

我在我正在处理的项目中遇到了类似的问题,添加显式 ModelBinding 属性使属性值保持不变

请求的数据:

var customer  = { Name : "customer Name", City : "custome City" }

$.ajax({ 
   url : ...
   type: ...
   data : customer
});

请求类:

public class Customer
{
    public string Name { get; set; }
    public string City { get; set; }
}

控制器:

public class UserController : ApiController
{
    [HttpGet]
    public Customer Get([ModelBinder] Customer user)
    {
        // fetch from whereever
    }
}

【讨论】:

    【解决方案2】:

    我现在正在经历同样的问题。我不是 100% 确定答案,但下面是我的 javascript,我已添加到类 [DataModel] 和属性 [DataMember]。那就是:

        [DataModel]
        public class Customer
        {
          [DataMember] public string Name { get; set; }
          [DataMember] public string City { get; set; }
        }
    

    还有我的 JavaScript

            $(document).ready(function () {
            // Send an AJAX request
            $.getJSON("api/session/GetAll",
            function (data) {
                // On success, 'data' contains a list of products.
                $.each(data, function (key, val) {
    
                    //debugger;
    
                    // Format the text to display.
                    //var str = val.Name + ': $' + val.Price;
                    var str = 'abcd';
    
                    // Add a list item for the product.
                    $('<li/>', { text: str })
                    .appendTo($('#products'));
                });
            });
        });
    

    【讨论】:

      【解决方案3】:

      遇到了类似的问题,我的问题原来是请求正文中的 JSON 无效。在其中一个字段之后缺少逗号。因此,如果 JSON 中有任何语法错误,默认模型绑定器似乎只是绑定 null。

      【讨论】:

        【解决方案4】:

        下次发生这种情况时,请确保属性设置器上没有“内部”关键字。那是: 而不是public string Comment {get; internal set;} 使用public string Comment {get; set;}

        这为我解决了问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多