代码之家  ›  专栏  ›  技术社区  ›  Steven Frank

ASP.NETMVC代码模型与Json正文绑定失败

  •  0
  • Steven Frank  · 技术社区  · 5 年前

    我正在尝试获取一个Json数组的JQuery.ajax post,以便与ASP.NET核心MVC控制器操作,无法使模型绑定工作。

    这个问题是下游的,但我包括它的完整性。

    $.ajax({
        url: "/Campaign/RosterMaintenanceAttribute",
        type: "post",
        data: JSON.stringify(attributeData),
        contentType: 'application/json; charset=utf-8',
        success: function (result) {
            alert(result);
        },
        error: function (request) {
            alert(request);
        }
    });
    

    以下是传出请求在浏览器中的外观:

    enter image description here

    enter image description here

    public IActionResult RosterMaintenanceAttribute([FromBody] IEnumerable<AttributeModel> attributes)
    

    我已经尝试将参数作为字符串,但是无论使用什么数据类型,参数都解析为null。很明显,这是一个MVC控制器,而不是Api控制器,我不确定[FromBody]是否在MVC控制器中工作。

    AttributeModel类如下所示:

    public class AttributeModel
    {
        public Guid CharacterAttributeId { get; set; }
        public Guid CharacterId { get; set; }
    
        public Guid AttributeTypeId { get; set; }
        public string AttributeName { get; set; }
        public string AttributeValue { get; set; }
        public int AttributeModifier { get; set; }
        public string AttributeNote { get; set; }
        public int AttributeOrdinal { get; set; }
        public bool Proficient { get; set; }
    }
    

    这就是使用.attributes语法时有效负载的样子 enter image description here

    这就是请求在控制器内部的样子 enter image description here

    0 回复  |  直到 5 年前
        1
  •  0
  •   Steven Frank    5 年前

    问题与字段的数据类型有关。似乎modelbinder不喜欢映射到int和bools,但是guid还可以(这些是我必须处理的3个,其他的不确定)。我已经验证了试图映射到int

    我决定发送不带根元素的数组

    data: JSON.stringify(attributeData.attributes)
    

    http://json2csharp.com/ )然后简单地粘贴到浏览器请求中的主体中,生成以下内容:

    public class httpAttributeModel
    {
        public string CharacterAttributeId { get; set; }
        public string CharacterId { get; set; }
        public string AttributeTypeId { get; set; }
        public string AttributeOrdinal { get; set; }
        public string AttributeName { get; set; }
        public string AttributeValue { get; set; }
        public string AttributeModifier { get; set; }
        public string Proficient { get; set; }
        public string AttributeNote { get; set; }
    }
    

    具有此控制器签名:

    public IActionResult RosterMaintenanceAttribute([FromBody] List<httpAttributeModel> attributes)
    

    这个有效。所以呢我开始调整数据类型,以尝试匹配用于接收有效负载的原始模型,查看失败的地方。int和bool的实际值对于类型都是有效的(stringify中围绕它们的引号除外),但这就是停止工作的地方。我不知道客户端是否需要做些什么来表示数据类型,或者更可能的是,服务器端是否需要做些什么来帮助modelbinder?对于这个问题,这已经足够好了,我可以在控制器中转换int和bool值。

    在我的最终解决方案中,在上面的模型中,我将前三个字段设置为guid,其余字段设置为原样。

        2
  •  0
  •   Tracy Zhou    4 年前

    尝试添加数据类型:“json”

    $.ajax({
        url: "/Campaign/RosterMaintenanceAttribute",
        type: "post",
        data: JSON.stringify(attributeData),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            alert(result);
        },
        error: function (request) {
            alert(request);
        }
    });