构建器中我的模型的空引用(MVC)

时间:2015-11-18 13:37:07

标签: asp.net-mvc

我有一个ViewModel接口:

 public interface IViewModelBuilder<T>
    {

        T Build(SessionState stateData, Dictionary<string, object> args = null);
    }

我有一个实现该接口的构建器:

  public class RepairBuilder : IViewModelBuilder<RepairModel>
{
    public RepairModel Build(SessionState stateData, Dictionary<string, object> args = null)
    {
        //var T= new Dictionary<string, object> { { "vehicleid", vehicleid } };
        var claimCondition = stateData.Claim.GetClaimCondition();

        var repairModel = new RepairModel
        {
            RepairState = ClaimRules.GetRepairState(stateData.Claim, stateData.CurrentInterestedParty, claimCondition),
            Status = EstimateStatus.EstimateOk,
            Appointment = ClaimRules.GetActiveRelatedAppointment(stateData.Claim, stateData.CurrentInterestedParty),
            DaysToRepairs = string.Format(
                             "{0} {1}",
                             stateData.CurrentInterestedParty.Vehicle.Repair.DaysToRepair,
                             stateData.CurrentInterestedParty.Vehicle.Repair.DaysToRepair == 1 ? "Day" : "Days"),
            GrossTotalAmount = stateData.CurrentInterestedParty.Vehicle.Repair.GrossTotalAmount.ToString(CultureInfo.InvariantCulture),
           //TODO add estimate document logic
        };

        return repairModel;
    }

所以在行变量MyModel中,模型为空并且抛出异常。

我应该在之前初始化词典。如果我们这样做了吗?

我的代码在接口参数中添加dictionnary之前一直在运行。

我的控制器正在使用这样的构建器:

    public class RepairsController : BaseController
{
    private readonly IViewModelBuilder<RepairModel> _repairBuilder;

    public RepairsController(IViewModelBuilder<RepairModel> repairBuilder, ISessionState stateData, IHttpRequestHelper httpRequest)
        : base(stateData,httpRequest)
    {
        this._repairBuilder = repairBuilder;

    }


 public ActionResult Estimate(string claimNumber, string vehicleid)
    {

        var repairStateModel = this._repairBuilder.Build(this.StateData,new Dictionary<string, object>{{"vehicleid",vehicleid}});
        return this.View("Estimate", repairStateModel);
    }
 }

0 个答案:

没有答案
相关问题