我将两个对象传递给我的控制器:
[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"
}
}),
答案 0 :(得分:1)
在您的JSON中,您传递的是具有属性Name
和Value
的对象的集合(请注意方括号):
"carModel": [ ... ]
但您的控制器操作需要一个CarQuoteRequestViewModel
。
因此,无论是修复您的JSON请求,还是想要发送多个汽车,请确保在控制器操作中使用正确的类型。
我在CarQuoteRequestViewModel
上看不到任何名称和值属性,因此即使您更改控制器操作以获取CarQuoteRequestViewModel
的集合,也不太可能这样做。您传递的JSON必须符合您的模型。
如果你想保留你的CarQuoteRequestViewModel
,你应该修复你的JSON:
"carModel": {
"carInfoViewModel": {
...
},
"driverInfoViewModel": {
...
}
}
更新:
看起来您正在尝试将某个表单元素($('#etape')
)序列化为JSON。您不应该使用serializeArray
方法,因为此方法会生成不正确的JSON,正如我已经解释过的那样。你得到一个名称/价值集合。您可以使用this post
中显示的serializeObject
方法。