ASP。NET MVC -值类型的服务器端验证

时间:2022-12-09 13:45:55

I have a model that contains value types (e.g. bool, DateTime, enums, etc) and I've marked the properties with the [Required] attribute.

我有一个包含值类型的模型(例如bool、DateTime、enums等),我已经用[Required]属性标记了属性。

I load the page, don't supply a value for anything, and submit the form. Client-side validation is turned off. Obviously the ModelState is invalid.

我加载页面,不提供任何值,然后提交表单。客户端验证被关闭。很明显,模型状态是无效的。

When the form is returned to the client however, these fields are populated with the type's default value (e.g. false, DateTime.MinValue, the first enum value, etc). This is not really what I want, I want the fields to stay empty.

然而,当表单返回给客户端时,这些字段将使用类型的默认值(例如false, DateTime)填充。MinValue、第一枚举值等)。这不是我真正想要的,我想让字段保持为空。

Currently I'm getting round this by making the properties nullable (e.g. bool?). Is this the "correct" thing to do? Or should I be doing something else to ensure that MVC isn't automatically populating value-type properties with the default value?

目前我通过使属性为空(例如bool?)来解决这个问题。这是正确的做法吗?或者我应该做一些其他的事情来确保MVC没有自动地用默认值填充值类型的属性吗?

1 个解决方案

#1


1  

I'm assuming you want the fields in the form to remain empty on next visit - in which case Nullable types is one way to go yes. Strings is another.

我假设您希望表单中的字段在下次访问时保持为空——在这种情况下,可以使用Nullable类型。字符串是另一个。

Note: I tend not to use Enums in my ViewModel, but rather nullable ints - I find it easier.

注意:我倾向于在我的ViewModel中不使用枚举,而是使用可空的ints——我发现这样更容易。

Another trick is to leave 0 for Enums to be used in Combo boxes unmapped, and then add a "Please Select" (value=0) option. In this case you are better off without a nullable type since 0 is useful.

另一个技巧是在没有映射的组合框中为枚举保留0,然后添加“请选择”(值=0)选项。在这种情况下,如果没有可空类型,那么您的情况会更好,因为0是有用的。

All of this works much better if you have a separate ViewModel from your Domain object - use Automapper of equivalent to map between them. This allows you to have one set of types in your ViewModel designed for your UI, without compromising what you have in your Domain Layer.

如果你有一个独立的视图模型和你的域对象——使用等价的Automapper在它们之间进行映射,那么所有这些都可以做得更好。这允许您在ViewModel中有一组为UI设计的类型,而不影响域层中的类型。

#1


1  

I'm assuming you want the fields in the form to remain empty on next visit - in which case Nullable types is one way to go yes. Strings is another.

我假设您希望表单中的字段在下次访问时保持为空——在这种情况下,可以使用Nullable类型。字符串是另一个。

Note: I tend not to use Enums in my ViewModel, but rather nullable ints - I find it easier.

注意:我倾向于在我的ViewModel中不使用枚举,而是使用可空的ints——我发现这样更容易。

Another trick is to leave 0 for Enums to be used in Combo boxes unmapped, and then add a "Please Select" (value=0) option. In this case you are better off without a nullable type since 0 is useful.

另一个技巧是在没有映射的组合框中为枚举保留0,然后添加“请选择”(值=0)选项。在这种情况下,如果没有可空类型,那么您的情况会更好,因为0是有用的。

All of this works much better if you have a separate ViewModel from your Domain object - use Automapper of equivalent to map between them. This allows you to have one set of types in your ViewModel designed for your UI, without compromising what you have in your Domain Layer.

如果你有一个独立的视图模型和你的域对象——使用等价的Automapper在它们之间进行映射,那么所有这些都可以做得更好。这允许您在ViewModel中有一组为UI设计的类型,而不影响域层中的类型。