首页 > 解决方案 > 循环通过控制器内的模型类

问题描述

这篇文章与嵌套的 Try/Catch 块有关

这是我的模型:

 public class Benefits
    {
        public int Id { get; set; }
        public Guid? ResponseId { get; set; }
        public decimal? MedicalTotal { get; set; }
        public decimal? StdSicknessAccident { get; set; }
        public decimal? LtdWage { get; set; }
        public decimal? MedicalPremiums { get; set; }
        public decimal? DentalPremiums { get; set; }
        public decimal? VisionCare { get; set; }
        public decimal? RetireePremiums { get; set; }
        public decimal? LifeInsurance { get; set; }
        public decimal? Prescription { get; set; }
        public decimal? MedAdmin { get; set; }
        public decimal? MedOther { get; set; }
        public decimal? HsaFsa { get; set; }
        public decimal? PtoTotal { get; set; }
        public decimal? Holidays { get; set; }
        public decimal? Pto { get; set; }
        public decimal? Vacations { get; set; }
        public decimal? SickLeave { get; set; }
        public decimal? PtoOther { get; set; }
        public decimal? RetirementTotal { get; set; }
        public decimal? X401k {get; set; }
        public decimal? DefinedBenefit { get; set; }
        public decimal? CashBalance { get; set; }
        public decimal? RetirementAdmin { get; set; }
        public decimal? RetirementOther { get; set; }
        public decimal? MiscTotal { get; set; }
        public decimal? Severance { get; set; }
        public decimal? Dependent { get; set; }
        public decimal? Tuition { get; set; }
        public decimal? Relocation { get; set; }
        public decimal? Total { get; set; }
    }

用户正在以相同的顺序上传具有行的 Excel 文件(他们没有上传自己的 Id 或 ResponseId。我已经开始编写 if 语句来捕获以下错误:

                    bool success = Decimal.TryParse(table[0], out decimal MedicalTotal);
                    if (success)
                    {
                        b.MedicalTotal = MedicalTotal;
                    }
                    else
                    {
                        model.ErrorList.Add("Medical total cell should be formatted as Currency, Accounting, or Number.");
                    }


                    bool success1 = Decimal.TryParse(table[1], out decimal StdSicknessAccident);
                    if (success1)
                    {
                        b.StdSicknessAccident = StdSicknessAccident;
                    }
                    else
                    {
                        model.ErrorList.Add("STD, Sickness, and Accident Insurance cell should be formatted as Currency, Accounting, or Number.");
                    }



                    bool success2 = Decimal.TryParse(table[2], out decimal LtdWage);
                    if (success2)
                    {
                        b.LtdWage = LtdWage;
                    }
                    else
                    {
                        model.ErrorList.Add("LTD & Wage Insurance cell should be formatted as Currency, Accounting, or Number.");
                    }

                    bool success3 = Decimal.TryParse(table[3], out decimal MedicalPremiums);
                    if (success3)
                    {
                        b.MedicalPremiums = MedicalPremiums;
                    }
                    else
                    {
                        model.ErrorList.Add("Medical Premiums cell should be formatted as Currency, Accounting, or Number.");
                    }

这是一个非常简单的过程,但我觉得,鉴于它的重复性,可能有一些方法可以循环我的模型字段并完成相同的任务。我可以[Display(Name="Medical Total")]向每个模型字段添加一个或一些其他元数据。我还考虑在我的上下文中添加一个包含所有错误消息的新表(这是最佳实践吗?)。

那么有没有比我的解决方案更短的替代方案?就像是:

List<bool> success = new List<bool>();
for(var i = 1; i<Benefits.FieldCount; i++)
{
    success[i] = decimal.TryParse(Table[i], out decimal Benefits.Field[i]
};
if(success[i])
{
    b.Field[i] = Benefits.Field[i];
}
else
{
    model.ErrorList.Add(Benefits.Field[i].Name "must be formatted as Accounting, Currency, or Number.");
}

标签: c#asp.net-mvc

解决方案


我会放弃所有的复杂性,只使用自定义验证属性

public class Foo : ValidationAttribute
{
    private readonly string _name;

    public Foo(string name)
    {
        _name = name;
    }

    protected override ValidationResult IsValid(object value, ValidationContext context)
    {
        // if the value is null, don't go further
        if (value == null) return ValidationResult.Success;

        // if the value cannot be parsed as a decimal (not valid)
        if (!Decimal.TryParse(value, out decimal d))
        {
            // return an error message
            return new ValidationResult($"{_name} cell should be formatted as...");
        }

        // if the parsed decimal is negative
        if (d < 0)
        {
            // return an error message
            return new ValidationResult($"{_name} cell cannot be negative.");
        }

        // if we got this far it was a success
        return ValidationResult.Success;
    }        
}

然后简单地用验证属性装饰你的属性

[Foo("STD, Sickness, and Accident Insurance")]
decimal? StdSicknessAccident { get; set; }

[Foo("LTD & Wage Insurance")]
decimal? LtdWage { get; set; }

推荐阅读