整数值的必需属性 [英] Required attribute for an integer value

查看:85
本文介绍了整数值的必需属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有ID属性的viewmodel

I have a viewmodel with an Id property

[Required]
public int Id { get; set; }

但是我认为该属性仅适用于字符串属性.

But I think this attribute is working only for string properties.

如果未设置ID,则ID的值为0,并且模型有效.

When no Id is set, Id has value 0 and the model is valid.

如果未设置int属性的值,该模型将无效吗?

How can I enforce that if no value for a int property is set, the model will be invalid ?

推荐答案

将类型更改为

Change the type to Nullable<int> (shortcut int?) to allow null values.

这篇关于整数值的必需属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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