在C#MVC中验证枚举值.发生部分验证-如何更改验证行为? [英] Validating Enum Values within C# MVC. Partial validation occurs - How to change validation behaviour?

查看:346
本文介绍了在C#MVC中验证枚举值.发生部分验证-如何更改验证行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在剃刀视图中将枚举表示为隐藏字段,并将其发布回操作结果.

I've been representing an enum within my razor view as a hidden field, which is posted back to an action result.

我注意到,当它绑定HTML中提供的字符串值时,它会自动验证枚举的值.

I've noticed that when it binds the string value provided within the HTML, it automatically validates the value for the enum.

/// <summary>
/// Quiz Types Enum
/// </summary>
public enum QuizType
{
    /// <summary>
    /// Scored Quiz
    /// </summary>
    Scored = 0,

    /// <summary>
    /// Personality Type Quiz
    /// </summary>
    Personality = 1
}

剃刀:

@Html.HiddenFor(x => x.QuizType)

呈现的HTML:

<input data-val="true" data-val-required="Quiz Type is not valid" id="QuizType" name="QuizType" type="hidden" value="Scored">

如果我将DOM中的值更改为不正确的值并提交表单,则ModelState.IsValid返回false,并且以下错误被添加到ModelState中:

If I change the value within the DOM to something incorrect and submit the form, ModelState.IsValid returns false and the following error is added to the ModelState:

"The value 'myincorrectvalue' is not valid for QuizType."

这很重要,但是我认为如果创建视图模型,则必须在视图模型上显式设置验证规则,例如[Required]属性.

That's all gravy, but I thought that if I created a view model, that I had to explicitly set validation rules on my view model, such as the [Required] attribute.

还有一个专门用于此的验证属性,称为EnumDataType.

Also there is a validation attribute specifically for this called EnumDataType.

[EnumDataType(typeof(QuizType))]
public QuizType QuizType { get; set; }

问题

如果绑定时自动进行验证,那么EnumDataType数据验证属性有什么意义?

If validation happens automatically when binding, what is the point in the EnumDataType data validation attribute?

推荐答案

好,所以我已经找到了自己问题的答案.

Ok, so I've found out the answer to my own question.

出现的错误消息更多是关于无法绑定的通用错误消息.当绑定尝试绑定从HTML发布到枚举的枚举值的不存在字符串表示形式时,会产生错误:

The error message that appeared is more of a generic error message for when the binding wasn't possible. When the binding attempts to bind a non-existent string representation of the enum value posted from the HTML to the enum, it produces the error:

The value 'myincorrectvalue' is not valid for QuizType.

如果我尝试将字符串值绑定到View Model类中的int,则会出现完全相同的错误消息.

The exact same error message would appear if I were to attempt to bind a string value to an int within my View Model class.

似乎问题在于,除了字符串表示形式外,枚举还可以是任何整数值.我可以将枚举设置为任意数字,即使该数字在我的枚举中不存在.

It seems that the issue is that as well as the string representation, an enum can be any integer value. I can set the enum to any number, even if that number doesn't exist within my enum.

/// <summary>
/// Quiz Types Enum
/// </summary>
public enum QuizType
{
    /// <summary>
    /// Scored Quiz
    /// </summary>
    Scored = 0,

    /// <summary>
    /// Personality Type Quiz
    /// </summary>
    Personality = 1
}

因此,这是有效的,即使1000在我的枚举中不存在,它也将毫无错误地绑定到我的枚举值:

Therefore, this is valid and will be bound without error to my enum value, even though 1000 doesn't exist within my enum:

<input data-val="true" id="QuizType" name="QuizType" type="hidden" value="1000">

//  Binder will bind this just fine
QuizType = 1000

这就是EnumDataType验证属性的来源.如果我将验证属性添加到视图模型中的枚举中,则:

That's where the EnumDataType validation attribute comes in. If I add the validation attribute to my enum within my view model:

[EnumDataType(typeof(QuizType), ErrorMessage = "Quiz type value doesn't exist within enum")]
public QuizType QuizType { get; set;}

有了该属性,我将只能分配有效的枚举值(在此示例中为0或1).

With the attribute in place, I will only be able to assign my valid enum values (0 or 1 for this example).

因此,绑定后会自动为您验证从HTML发布的不正确的STRING表示形式,但不会检查任何整数值.

So the incorrect STRING representations posted from the HTML is validated for you automatically upon binding, but a check for any integer value will not.

我希望这可以清除ASP.NET MVC中的ENUMS验证.

I hope that this clears up validating ENUMS within ASP.NET MVC.

这篇关于在C#MVC中验证枚举值.发生部分验证-如何更改验证行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆