控制器不会将json转换为视图模型



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

[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。你知道什么可能是错误的CarQuoteRequestViewModel映射从JSON ?

编辑

根据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"
        }
            }),

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

"carModel": [ ... ]

但是你的控制器动作期望一个CarQuoteRequestViewModel

所以要么修复你的JSON请求,要么如果你想发送多辆车,确保你在控制器动作上使用正确的类型。

我没有看到任何名称和值属性在你的CarQuoteRequestViewModel,所以即使你改变你的控制器动作采取CarQuoteRequestViewModel的集合是不太可能的,这将是工作。你传递的JSON必须符合你的模型。

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

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

更新:

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

相关内容

  • 没有找到相关文章

最新更新