0

我将两个对象传递给我的控制器:

[HttpPost]
public ActionResult GetSelectedQuote(CarQuoteRequestViewModel carModel, QuoteViewModel quoteModel)
 {
      return PartialView("_QuoteSelected", quoteModel);
 }

AJAX 调用如下(我对 JSON 字符串进行了硬编码,因此您可以看到它的外观,但通常我会使用 ser $.ajax("getSelectedQuote", {

            //data: ko.toJSON({ model: self.selectedQuote, model1: $("#etape").serializeArray() }),
            data:  ko.toJSON({"quoteModel":{"ProductName":"Classic","MonthPrice":98.19,"QuarterPrice":291.64,"BiannualPrice":577.37,"YearPrice":1142.94,
    "CoverQuotesViewModel":[
    {"ProductName":"Classic","Label":"Responsabilité Civile","IsVisible":false,"IsMandatory":false,"IsSelected":false,"YearPrice":338.17,"BiannualPrice":170.83,"QuarterPrice":86.29,"MonthPrice":29.05},        
    {"ProductName":"Classic","Label":"Première Assistance 24h/24 (GRATUITE)","IsVisible":false,"IsMandatory":false,"IsSelected":false,"YearPrice":0,"BiannualPrice":0,"QuarterPrice":0,"MonthPrice":0},
    {"ProductName":"Classic","Label":"Dommage (DM TCC 0%)","IsVisible":false,"IsMandatory":false,"IsSelected":false,"YearPrice":717.47,"BiannualPrice":362.44,"QuarterPrice":183.07,"MonthPrice":61.64},
    {"ProductName":"Classic","Label":"Individuelle Circulation","IsVisible":false,"IsMandatory":false,"IsSelected":false,"YearPrice":67.9,"BiannualPrice":34.3,"QuarterPrice":17.33,"MonthPrice":5.83},
    {"ProductName":"Classic","Label":"Protection Juridique","IsVisible":false,"IsMandatory":false,"IsSelected":false,"YearPrice":19.4,"BiannualPrice":9.8,"QuarterPrice":4.95,"MonthPrice":1.67}
    ]},

"carModel": [
        {"name":"DriverInfoViewModel.DriverInfo.NoClaimsDegree","value":"1"},
            {"name":"DriverInfoViewModel.DriverInfo.DrivingLicenceDate","value":"2013-03-02"},
            {"name":"DriverInfoViewModel.DriverInfo.DisasterHistory","value":"MoreThanTwoDisasters"},
            {"name":"CarInfoViewModel.CarInfo.CarValue","value":"15000"},
            {"name":"CarInfoViewModel.CarInfo.AudioValue","value":"1000"},
            {"name":"CarInfoViewModel.CarInfo.EngineCapacity","value":"1500"},
            {"name":"CarInfoViewModel.CarInfo.AdditionalSeats","value":"1"},
            {"name":"CarInfoViewModel.CarInfo.FirstRegistration","value":"2013-03-02"}
       ]}),
            type: "post", contentType: "application/json",
            success: function (result) {
                $("#custom").html(result);
                $("#etape").formwizard("show", "customize");
            }
        });

奇怪的是,当控制器被击中时,QuoteViewModel 完全重建。但是 CarQuoteRequestViewModel 对象不是。属性为空。

ViewModel 对象如下:

public class CarQuoteRequestViewModel : QuoteRequestViewModel
{       
    public CarInfoViewModel CarInfoViewModel { get; set; }
    public DriverInfoViewModel DriverInfoViewModel { get; set; }

    public CarQuoteRequestViewModel()
    {
        CarInfoViewModel = new CarInfoViewModel();
        DriverInfoViewModel = new DriverInfoViewModel();
    }
}


public class QuoteViewModel
{
    public string ProductName { get; set; }
    public decimal MonthPrice { get; set; }
    public decimal QuarterPrice { get; set; }
    public decimal BiannualPrice { get; set; }
    public decimal YearPrice { get; set; }

    public List<CoverQuoteViewModel> CoverQuotesViewModel { get; set; }
}

我暂时不发布所有嵌套对象,因为它适用于 QuoteViewModel。您知道来自 JSON 的 CarQuoteRequestViewModel 映射可能有什么问题吗?

编辑

根据 Darin 的建议,我已将 JSON 更改为:

  data: ko.toJSON({ "model": { "ProductName": "Classic", "MonthPrice": 98.19, "QuarterPrice": 291.64, "BiannualPrice": 577.37, "YearPrice": 1142.94,
                "CoverQuotesViewModel": [
    { "ProductName": "Classic", "Label": "Responsabilité Civile", "IsVisible": false, "IsMandatory": false, "IsSelected": false, "YearPrice": 338.17, "BiannualPrice": 170.83, "QuarterPrice": 86.29, "MonthPrice": 29.05 },
    { "ProductName": "Classic", "Label": "Première Assistance 24h/24 (GRATUITE)", "IsVisible": false, "IsMandatory": false, "IsSelected": false, "YearPrice": 0, "BiannualPrice": 0, "QuarterPrice": 0, "MonthPrice": 0 },
    { "ProductName": "Classic", "Label": "Dommage (DM TCC 0%)", "IsVisible": false, "IsMandatory": false, "IsSelected": false, "YearPrice": 717.47, "BiannualPrice": 362.44, "QuarterPrice": 183.07, "MonthPrice": 61.64 },
    { "ProductName": "Classic", "Label": "Individuelle Circulation", "IsVisible": false, "IsMandatory": false, "IsSelected": false, "YearPrice": 67.9, "BiannualPrice": 34.3, "QuarterPrice": 17.33, "MonthPrice": 5.83 },
    { "ProductName": "Classic", "Label": "Protection Juridique", "IsVisible": false, "IsMandatory": false, "IsSelected": false, "YearPrice": 19.4, "BiannualPrice": 9.8, "QuarterPrice": 4.95, "MonthPrice": 1.67 }
    ]
            },

                "model1":
        { "DriverInfoViewModel.DriverInfo.NoClaimsDegree" : "1",
            "DriverInfoViewModel.DriverInfo.DrivingLicenceDate" : "2013-03-02",
             "DriverInfoViewModel.DriverInfo.DisasterHistory" : "MoreThanTwoDisasters",
            "CarInfoViewModel.CarInfo.CarValue": "15000",
            "CarInfoViewModel.CarInfo.AudioValue" : "1000",
            "CarInfoViewModel.CarInfo.EngineCapacity" : "1500",
            "CarInfoViewModel.CarInfo.AdditionalSeats" : "1",
            "CarInfoViewModel.CarInfo.FirstRegistration" : "2013-03-02"
        }
            }),
4

1 回答 1

1

在您的 JSON 中,您正在传递具有属性Name和的对象的集合(注意方括号) Value

"carModel": [ ... ]

但是您的控制器操作需要一个CarQuoteRequestViewModel.

因此,要么修复您的 JSON 请求,要么如果您想发送多辆汽车,请确保您在控制器操作中使用正确的类型。

我在您的 上看不到任何 Name 和 Value 属性CarQuoteRequestViewModel,因此即使您更改控制器操作以收集CarQuoteRequestViewModel它也不太可能起作用。您传递的 JSON 必须符合您的模型。

如果你想保留你的CarQuoteRequestViewModel,你应该修复你的 JSON:

"carModel": {
    "carInfoViewModel": {
        ...
    },
    "driverInfoViewModel": {
        ...
    }
}

更新:

看起来您正试图将某些表单元素 ( $('#etape')) 序列化为 JSON。您不应该使用该serializeArray方法,因为正如我已经解释过的那样,该方法会生成不正确的 JSON。你得到一个名称/值集合。您可以使用中serializeObject所示的方法this post

于 2013-03-08T15:22:17.100 回答