【问题标题】:IValidatableObject implementation does not workIValidatableObject 实现不起作用
【发布时间】:2015-10-15 16:24:07
【问题描述】:

在我的配方线上,我想创建一些我使用 IValidateObject 接口的验证逻辑。运行后我仍然收到一条黄色错误消息,并且在调试时我注意到 Validat 函数甚至没有被调用。

我希望有人能解释我如何才能使验证正常工作。

 public class RecipeLine : IValidatableObject
    {
        [Display(Name = "Receptregel")]
        public string QuantityUomIngredient => $"{Quantity} {UnitOfMeasure?.Abbreviation ?? ""} {Ingredient?.Name ?? ""}";

    private RecipeApplicationDb db = new RecipeApplicationDb();

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        // Split the incoming string
        string[] valueAsString = QuantityUomIngredient.Split();

        if (QuantityUomIngredient != null)
        {
            // Check if the string has the proper length
            if (valueAsString.Length < 3)
            {
                var StringErrorMessage = "Er zijn onvoldoende gegevens ingevuld. voorbeeld: 1,2 kg Aardappelen";
                yield return new ValidationResult(StringErrorMessage);
            }

            // Check if the first value of the string is a double
            double quantityValue;
            bool quantity = double.TryParse(valueAsString[0], out quantityValue);
            if (!quantity)
            {
                var QuantErrorMessage = "De hoeveelheid moet een numerieke waarde zijn.";
                yield return new ValidationResult(QuantErrorMessage);
            }

            // Check if the UOM value exists in the database
            string uom = valueAsString[1];
            bool checkUOM = (from x in db.UnitOfMeasures where x.Abbreviation.ToLower() == uom select x).Count() > 0;

            if (!checkUOM)
            {
                var UomErrorMessage = "Er is geen juiste maateenheid ingevoerd.";
                yield return new ValidationResult(UomErrorMessage);
            }

            // Check if the ingredient exists in the database
            string ingredient = valueAsString[2];
            bool checkIngredient = (from x in db.Ingredients where x.Name.ToLower() == ingredient.ToLower() select x).Count() > 0;
            if (!checkIngredient)
            {
                var IngredientErrorMessage = "Er is geen juist ingredient ingevoerd.";
                yield return new ValidationResult(IngredientErrorMessage);
            }
        }
    }
}

提前致谢。

==================编辑======================

也许这也很重要。在控制器中,我放置了一个自定义模型绑定器。调试时,我注意到我无法使用验证功能,但它直接进入 db.savechanges。

    // POST: RecipeLine/Create
    [HttpPost]
    public ActionResult Create([ModelBinder(typeof(RecipeLineCustomBinder))] RecipeLine recipeLine)
    {
        ViewBag.ingredients = (from x in db.Ingredients select x).ToList();
        ViewBag.uom = (from x in db.UnitOfMeasures select x).ToList();
        if (ModelState.IsValid)
        {
            db.RecipeLines.Add(recipeLine);
            db.SaveChanges();
            return RedirectToAction("Create", new { id = recipeLine.RecipeId });
        }
        return View(recipeLine);
    }

【问题讨论】:

    标签: c# asp.net validation


    【解决方案1】:

    IValidatableObject 仅在属性验证成功时使用。

    【讨论】:

    猜你喜欢
    • 2012-01-29
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多