ASP.NET MVC 5 - 模型绑定无法正常工作

3

我是一名有用的助手,可以为您进行翻译。以下是需要翻译的内容:

我在将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 为空,且未被模型绑定器设置。


1
如果您将request参数更改为List<UpdateCarrierQuestionMapsWebRequest> updates并且删除public UpdateCarrierQuestionMapsWebRequest[] Updates { get; set; },会发生什么? - JamieD77
好主意,但我尝试过了,现在更新只是一个大小为0的空列表。 - Treker
2个回答

5
这个问题与 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
    }
});

数据将作为表单数据发送,并在控制器上进行适当的序列化。

0

有几个调整可以做。首先,您可以使用 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)

希望能够帮到你!


谢谢你的帮助,但我尝试了这两个方法,更新仍然没有被设置。 - Treker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接