【问题标题】:Post an array of complex objects with JSON, MVC model is do not bind使用 JSON 发布复杂对象数组,MVC 模型不绑定
【发布时间】:2017-08-07 17:40:00
【问题描述】:

这是我的代码。

json_model

var mix = {
        MixName: $("#mixname").val(),
        MixDesc: tinyMCE.activeEditor.getContent(),
        Price: $("#price").val(),
        DiseaseMixs: [],
        MixProducts: []
    }

将项目添加到 DiseaseMixs 和 MixProducts

$("#DiseaseList").find("tbody tr").each(function (index) {
        mix.DiseaseMixs.push({
            MixID: parseInt(MixID),
            DiseaseID: parseInt($(".diseaseid").eq(index).html()),
            ImpactDegree: parseInt($(".derece option:selected").eq(index).html()),
            Description: $(".diseaseMixDesc input").eq(index).val()
        });
    })
    $("#productList").find("tbody tr").each(function (index) {
        mix.MixProducts.push({
            MixID: parseInt(MixID),
            ProductID: parseInt($(".productid").eq(index).html()),
            MeasureTypeID: parseInt($(".birim option:selected").eq(index).val()),
            MeasureAmount: $(".measureAmount input").eq(index).val()
        });
    })

这个过程结束,这里是一个示例 json 对象,它是 post。

{
"MixName": "asdasddas",
"MixDesc": "<p>sadasd</p>",
"Price": "123",
"DiseaseMixs": [{
    "MixID": 1,
    "DiseaseID": 2,
    "ImpactDegree": 5,
    "Description": "asads"
}, {
    "MixID": 1,
    "DiseaseID": 3,
    "ImpactDegree": 4,
    "Description": "aqqq"
}],
"MixProducts": [{
    "MixID": 1,
    "ProductID": 2,
    "MeasureTypeID": 3,
    "MeasureAmount": "3"
}, {
    "MixID": 1,
    "ProductID": 3,
    "MeasureTypeID": 2,
    "MeasureAmount": "45"
}]
}

ajax 帖子

$.ajax({
        url: 'SaveMix',
        type: 'POST',
        data: JSON.stringify(mix),
        contentType: 'application/json; charset=utf-8',
        success: function (result) {
            console.log(result);
        },
        error: function (xhr, status) {
            alert(status);
            console.log(xhr);
        }


    })

以及MVC模型和JSONResult函数

型号

public class MixModel
{
    public string MixName { get; set; }
    public string MixDesc { get; set; }
    public float Price { get; set; }
    DiseaseMix[] DiseaseMixs { get; set; } //DiseaseMix EntityFramework entity
    MixProduct[] MixProducts { get; set; } //MixProduct EF

}

功能

[HttpPost]
    public JsonResult SaveMix(MixModel mix)
    {
        bool result = false;
        //do something

        return Json(new { result = result }, JsonRequestBehavior.AllowGet);
    }

这是我得到的结果。

无论我怎么尝试,我都无法绑定模型。

我做错了什么?请给我一些帮助。

提前致谢。

【问题讨论】:

    标签: arrays json asp.net-mvc model-binding


    【解决方案1】:

    模型绑定失败,因为这 2 个属性当前为 private(这是您未明确指定任何内容时的默认值)。

    将 2 个属性更改为 public,以便模型绑定器可以设置它们的值。

    public class MixModel
    {
        public string MixName { get; set; }
        public string MixDesc { get; set; }
        public float Price { get; set; }
        public DiseaseMix[] DiseaseMixs { get; set; } //DiseaseMix EntityFramework entity
        public MixProduct[] MixProducts { get; set; } //MixProduct EF
    
    }
    

    我还建议不要将您的视图模型与您的 ORM 生成的实体混合在一起。这创建了一个紧密耦合的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 2017-07-16
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多