问题描述
所以我试图做让在控制器的模型对象的关键,这样我可以添加 AddModelError
来了。
在我看来,我用
@ Html.ValidationMessageFor(型号=> model.Email)
请告诉我,相当于code,以获得键
名称在控制器添加所以重视这一ValidationMessage。
ModelState.AddModelError(电子邮件,电子邮件是无效的);
但通常这不是你应该做手动在你的控制器,但你应该使用验证器。例如,你可以装点这个电子邮件属性与一些验证数据注解属性,或者如果你像我一样使用 =>这样,你不应该问自己有关键的问题,但着眼于实际的验证逻辑。
So I am trying to do get the key for a model object in the controller so that I can add a AddModelError
to it.
In my view I use
@Html.ValidationMessageFor(model => model.Email)
Whats the equivalent code to get the Key
name to add in the controller so it attaches to this ValidationMessage.
ModelState.AddModelError("Email", "the email is invalid");
But usually that's not something you should be doing manually in your controller but you should be using a validator. For example you could decorate this Email property with some validation data annotation attribute or if you are like me use FluentValidation.NET => this way you shouldn't be asking yourself questions about keys but focus on the actual validation logic.
这篇关于获得"关键"为在所述控制器的强类型模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!