【问题标题】:ASP.NET MVC 5 - Model Binding Not WorkingASP.NET MVC 5 - 模型绑定不起作用
【发布时间】:2018-06-18 22:02:05
【问题描述】:

我在将 HTTP POST 请求正文中的传入 JSON 数据绑定到我的 C# 模型时遇到问题。

这是我的前端 JavaScript 代码:

let jsonData = "{\"Updates\":[{\"CarrierStateMapGuid\":\"de4abaa8-42d2-4e00-657a08d5577ac94a\",\"QuestionTag\":\"CoQstPAVT500006\",\"MemberOf\":\"Quote\",\"Condition\":\"0\",\"QuestionType\":\"List\",\"TrueAnswer\":\"NoDiscount\",\"TrueExplanation\":\"No Discount\",\"FalseAnswer\":null,\"FalseExplanation\":null,\"DeleteRequest\":false}]}";
$.ajax({
    url: "/api/CarrierQuestionMappingApi/UpdateQuestionMaps",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: jsonData
});

这是我的 C# 模型:

public class UpdateCarrierQuestionMapsWebRequests
{
    public UpdateCarrierQuestionMapsWebRequest[] Updates { get; set; }

    public class UpdateCarrierQuestionMapsWebRequest
    {
        public string CarrierStateMapGuid { get; set; }
        public string QuestionTag { get; set; }
        public string MemberOf { get; set; }
        public string Condition { get; set; }
        public string QuestionType { get; set; }
        public string TrueAnswer { get; set; }
        public string TrueExplanation { get; set; }
        public string FalseAnswer { get; set; }
        public string FalseExplanation { get; set; }
        public bool DeleteRequest { get; set; }
    }
}

这是我的后端 C# 控制器代码:

[HttpPost]
[Route("api/[controller]/UpdateQuestionMaps")]
public HttpResponseMessage UpdateQuestionMaps(UpdateCarrierQuestionMapsWebRequests request)
{
     // request.Updates is null
}

我无法弄清楚为什么 request.Updates 为 null 并且没有被模型绑定器设置。

【问题讨论】:

  • 如果将request 参数更改为List<UpdateCarrierQuestionMapsWebRequest> updates 并去掉public UpdateCarrierQuestionMapsWebRequest[] Updates { get; set; } 会怎样
  • 好主意,但我试过了,现在更新只是一个大小为 0 的空列表。

标签: javascript c# json asp.net-mvc


【解决方案1】:

问题与 AJAX 和 ASP.NET MVC 有关。 MVC 不喜欢 AJAX 的任何序列化。当您向 AJAX 传递一个对象时,它会手动对其进行序列化,并且 MVC 期望以 AJAX 对其进行序列化的方式对其进行反序列化。所以任何手动序列化都会破坏这个过程。在您上面的方法中,您最终会得到一个编码字符串。但是,如果您将 AJAX 调用更改为:

let jsonData = "[{\"CarrierStateMapGuid\":\"de4abaa8-42d2-4e00-657a08d5577ac94a\",\"QuestionTag\":\"CoQstPAVT500006\",\"MemberOf\":\"Quote\",\"Condition\":\"0\",\"QuestionType\":\"List\",\"TrueAnswer\":\"NoDiscount\",\"TrueExplanation\":\"No Discount\",\"FalseAnswer\":null,\"FalseExplanation\":null,\"DeleteRequest\":false}]";
$.ajax({
    url: "/api/CarrierQuestionMappingApi/UpdateQuestionMaps",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: {
         Updates: jsonData
    }
});

数据将作为表单数据发送并在控制器上正确序列化。

【讨论】:

    【解决方案2】:

    您可以进行一些调整。首先您可以使用List<T> 代替数组,然后在无参数构造函数中对其进行实例化:

    public class UpdateCarrierQuestionMapsWebRequests
    {
        public List<UpdateCarrierQuestionMapsWebRequest> Updates { get; set; }
    
        public UpdateCarrierQuestionMapsWebRequests()
        {
           Updates = new List<UpdateCarrierQuestionMapsWebRequest>();
        }
    
       public class UpdateCarrierQuestionMapsWebRequest
       {
            public string CarrierStateMapGuid { get; set; }
            public string QuestionTag { get; set; }
            public string MemberOf { get; set; }
            public string Condition { get; set; }
            public string QuestionType { get; set; }
            public string TrueAnswer { get; set; }
            public string TrueExplanation { get; set; }
            public string FalseAnswer { get; set; }
            public string FalseExplanation { get; set; }
            public bool DeleteRequest { get; set; }
        }
    }
    

    在您看来,在发送请求之前,您可以对 json 进行字符串化,如下所示:

    data: JSON.stringify(jsonData)
    

    希望对您有所帮助!

    【讨论】:

    • 感谢您的帮助,但我尝试了这两个方法,但更新仍未设置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    相关资源
    最近更新 更多