C# 实体框架中需要十进制字段
我有一个简单的asp.net mvc应用程序,我首先使用EF代码 我有一个“Student”类,它的属性类型为decimal,其他属性类型为int。我不知道为什么decimal属性在没有任何数据注释的情况下不为null 另一个问题是,当我运行我的应用程序时,带有decimal属性的文本框检索170,6的值,但当我提交它时,它显示一个错误,指出170,6不是有效值,我必须将其更改为170.6才能提交C# 实体框架中需要十进制字段,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我有一个简单的asp.net mvc应用程序,我首先使用EF代码 我有一个“Student”类,它的属性类型为decimal,其他属性类型为int。我不知道为什么decimal属性在没有任何数据注释的情况下不为null 另一个问题是,当我运行我的应用程序时,带有decimal属性的文本框检索170,6的值,但当我提交它时,它显示一个错误,指出170,6不是有效值,我必须将其更改为170.6才能提交 public class Student { public int StudentID {
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public DateTime? DateOfBirth { get; set; }
public byte[] Photo { get; set; }
public decimal Height { get; set; }
public int Weight { get; set; }
public int changement { get; set; }
public Grade Grade { get; set; }
}
public class Grade
{
public int GradeId { get; set; }
public string GradeName { get; set; }
public string Section { get; set; }
public ICollection<Student> Students { get; set; }
}
公共班级学生
{
公共int StudentID{get;set;}
公共字符串StudentName{get;set;}
公共日期时间?出生日期{get;set;}
公共字节[]Photo{get;set;}
公共十进制高度{get;set;}
公共整数权重{get;set;}
公共整数更改{get;set;}
公共等级{get;set;}
}
公营班级
{
public int GradeId{get;set;}
公共字符串GradeName{get;set;}
公共字符串部分{get;set;}
公共ICollection学生{get;set;}
}
我不明白为什么没有任何数据注释,decimal属性不为null
C#中的decimal是一种值类型。它不能是null
。例如,int
,double
,DateTime
170,6不是有效值,我必须将其更改为170.6才能提交
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public DateTime? DateOfBirth { get; set; }
public byte[] Photo { get; set; }
public decimal Height { get; set; }
public int Weight { get; set; }
public int changement { get; set; }
public Grade Grade { get; set; }
}
public class Grade
{
public int GradeId { get; set; }
public string GradeName { get; set; }
public string Section { get; set; }
public ICollection<Student> Students { get; set; }
}
这是一个常见的文化/地区问题。我正在寻找一个关于这个主题的重复问题,提供一个关于如何解决这个问题的有用链接
这有点棘手,因为MVC必须在客户端生成允许提交的验证代码,并且客户端可以配置其他区域性
编辑:你可以看看这个问题,它是公认的答案:
对于第一个问题,
decimal
是一种值类型,不能为null。如果希望它为空,则应将其定义为decimal?
(这是可空
引用类型包装的快捷方式)。确保数据库中的相应字段也是NULL
对于第二个问题,您应该确保您的请求线程与您的客户端在相同的区域性下运行。有很多方法可以做到这一点,试着在这个网站或谷歌上查找。除了notnull之外,
decimal
怎么可能是任何东西呢?要为空,您需要使用decimal?
。非常感谢