MVC Post动作没有正确绑定

时间:2014-08-12 18:11:04

标签: c# asp.net-mvc-4 model-binding

我的Scenario类型ScenarioMeasures模型中有3个属性。它们似乎正在POST,但ScenarioTargets数字用于所有3个属性。我不得不相信他们由于某种原因没有正确的约束力。为什么会这样?

模型

public class EditScenarioViewModel
{
    public Scenario scenario { get; set; }

    public IEnumerable<SelectListItem> locations { get; set; }

    public IEnumerable<SelectListItem> fiscalPeriods { get; set; }

    public IEnumerable<SelectListItem> effectivePeriods { get; set; }
}

public class Scenario
{
    //lots of other stuff, then...

    [Required]
    public ScenarioMeasures Adjustments { get; set; }

    public ScenarioMeasures BaselineData { get; set; }

    public ScenarioMeasures ScenarioTargets { get; set; }

    //more stuff...
}

public class ScenarioMeasures
{
    public decimal RGURate { get; set; }
    public decimal RevRate { get; set; }
    public int RevAmount { get; set; }
    public decimal VideoRate { get; set; }
    public decimal TSR { get; set; }
    public decimal Home { get; set; }
    public decimal CustomerRetentionRate { get; set; }
}

控制器

// POST: Scenario/Edit/5
[HttpPost]
public ActionResult Edit(int id, EditScenarioViewModel viewModel)
...

发布数据

scenario.BaselineData.RGURate:0.77
scenario.Adjustments.RGURate:0.01
scenario.ScenarioTargets.RGURate:0.78

scenario.BaselineData.RevRate:0.83
scenario.Adjustments.RevRate:0.02
scenario.ScenarioTargets.RevRate:0.85

scenario.BaselineData.RevAmount:37931
scenario.Adjustments.RevAmount:300
scenario.ScenarioTargets.RevAmount:38231

scenario.BaselineData.VideoRate:0.73
scenario.Adjustments.VideoRate:0.04
scenario.ScenarioTargets.VideoRate:0.77

scenario.BaselineData.TSR:0.02
scenario.Adjustments.TSR:0.05
scenario.ScenarioTargets.TSR:0.07

scenario.BaselineData.Home:0.00
scenario.Adjustments.Home:0.00
scenario.ScenarioTargets.Home:0.00

scenario.BaselineData.CustomerRetentionRate:0.00
scenario.Adjustments.CustomerRetentionRate:0.00
scenario.ScenarioTargets.CustomerRetentionRate:0.00

绑定数据

Adjustments     {BusinessModels.Models.ScenarioMeasures}      BusinessModels.Models.ScenarioMeasures
CustomerRetentionRate   0       decimal
Home                    0       decimal
RevAmount               38231   int
RevRate                 0.85    decimal
RGURate                 0.78    decimal
TSR                     0.07    decimal
VideoRate               0.77    decimal

BaselineData    {BusinessModels.Models.ScenarioMeasures}      BusinessModels.Models.ScenarioMeasures
CustomerRetentionRate   0       decimal
Home                    0       decimal
RevAmount               38231   int
RevRate                 0.85    decimal
RGURate                 0.78    decimal
TSR                     0.07    decimal
VideoRate               0.77    decimal

ScenarioTargets {BusinessModels.Models.ScenarioMeasures}      BusinessModels.Models.ScenarioMeasures
CustomerRetentionRate   0       decimal
Home                    0       decimal
RevAmount               38231   int
RevRate                 0.85    decimal
RGURate                 0.78    decimal
TSR                     0.07    decimal
VideoRate               0.77    decimal

1 个答案:

答案 0 :(得分:0)

如果有人在将来遇到这种情况......它似乎与在视图模型中嵌套相同类型的对象(在这种情况下,三个BusinessModels.Models.ScenarioMeasures)不止一个级别相关。

能够通过在viewmodel中执行单个字段来解决,然后复制这些&#34; transfer&#34;字段到控制器中的对象。这很难看,但它确实有效。