IValidatableObject实现不起作用

时间:2015-10-15 07:37:33

标签: c# asp.net validation

在我的食谱线上,我想创建一些验证逻辑,我使用了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);
            }
        }
    }
}

提前致谢。

================== EDIT ======================

也许这也很重要。在控制器中我放了一个自定义模型绑定器。在调试时,我注意到我无法进入验证功能,但它直接进入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);
    }

2 个答案:

答案 0 :(得分:0)

看看这个。

How do I use IValidatableObject?

我希望这会有所帮助,如果这不是您需要的,请发表评论。

答案 1 :(得分:0)

IValidatableObject仅在属性&#39;验证成功。

相关问题