ASP.NET MVC View类和null

时间:2017-09-29 18:53:05

标签: asp.net-mvc asp.net-mvc-5 automapper-6

我有一个带有嵌套视图模型类的复杂结构:

public class ApplicationDriverFormVM : IValidatableObject
{
    [Required]
    public string Sign { get; set; }

    public string Company { get; set; }

    public ApplicationDriverAddressFormVM PresentAddress { get; set; }
    public List<ApplicationDriverAddressFormVM> PreviousAddresses { get; set; }

    public ApplicationDriverLicenseFormVM License { get; set; }

    public ApplicationDriverEducationFormVM Education { get; set; }

    public List<ApplicationDriverAdditionalLicenseFormVM> AdditionalLicenses { get; set; }


    //public ApplicationDriverAccidentFormVM DefaultAccident { get; set; }
    public List<ApplicationDriverAccidentFormVM> Accidents { get; set; }

    public List<ApplicationDriverTrafficConvictionFormVM> TrafficConvictions { get; set; }

    public List<ApplicationDriverEmployerFormVM> Employers { get; set; }


    public ApplicationDriverEquipmentTractorFormVM EquipmentTractor { get; set; }
    public ApplicationDriverEquipmentTrailerFormVM EquipmentTrailer { get; set; }
    public ApplicationDriverEquipmentStraightTruckFormVM EquipmentStraightTruck { get; set; }
    public ApplicationDriverEquipmentCargoVanFormVM EquipmentCargoVan { get; set; }

    public ApplicationDriverFormVM()
    {
        PresentAddress = new ApplicationDriverAddressFormVM();
        PreviousAddresses = new List<ApplicationDriverAddressFormVM>();
        PreviousAddresses.Add(new ApplicationDriverAddressFormVM());
        License = new ApplicationDriverLicenseFormVM();
        Education = new ApplicationDriverEducationFormVM();
        AdditionalLicenses = new List<ApplicationDriverAdditionalLicenseFormVM>();
        AdditionalLicenses.Add(new ApplicationDriverAdditionalLicenseFormVM());
        Accidents = new List<ApplicationDriverAccidentFormVM>();
        Accidents.Add(new ApplicationDriverAccidentFormVM());
        TrafficConvictions = new List<ApplicationDriverTrafficConvictionFormVM>();
        TrafficConvictions.Add(new ApplicationDriverTrafficConvictionFormVM());
        Employers = new List<ApplicationDriverEmployerFormVM>();
        Employers.Add(new ApplicationDriverEmployerFormVM());
        EquipmentTractor = new ApplicationDriverEquipmentTractorFormVM();
        EquipmentTrailer = new ApplicationDriverEquipmentTrailerFormVM();
        EquipmentStraightTruck = new ApplicationDriverEquipmentStraightTruckFormVM();
        EquipmentCargoVan = new ApplicationDriverEquipmentCargoVanFormVM();
    }
}

每个嵌套类都可以有嵌套类......

问题是这些类中的许多类没有任何必需字段,因此,客户端可以将某些类的所有字段保留为空。结果,控制器得到这样的对象:

enter image description here

AutoMapper将此类映射到域(然后是基础结构)类,并且每个实体在DB中将其保存为适当表中的不同记录。但我不想保存空记录,我想不将它们映射到域类。怎么做?验证每个对象的所有属性是否为null或者有更强大的解决方案?

0 个答案:

没有答案