小编Sab*_*rma的帖子

如何使我的字符串属性可以为空?

我想让人的中间名可选.我一直在使用C#.net代码的第一种方法.对于整数数据类型,只需使用?运算符使其可以为空即可.我正在寻找一种让我的sting变量可以为空的方法.我试图搜索但找不到让它可以为空的方法.

以下是我的代码.请建议我如何让它可以为空.

public class ChildrenInfo
{
    [Key]
    public int ChidrenID { get; set; }

    [Required]
    [Display(Name ="First Name")]
    [StringLength(50,ErrorMessage ="First Name cannot exceed more than 50 characters")]
    [RegularExpression(@"^[A-Z]+[a-z]*$",ErrorMessage ="Name cannot have special character,numbers or space")]
    [Column("FName")]
    public string CFName { get; set; }

    [Display(Name ="Middle Name")]
    [RegularExpression(@"^[A-Z]+[a-z]*$",ErrorMessage ="Middle Name cannot have special character,numbers or space")]
    [StringLength(35,ErrorMessage ="Middle Name cannot have more than 35 characters")]
    [Column("MName")]
    public string? CMName { get; set; }
}   
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc entity-framework ef-migrations

44
推荐指数
4
解决办法
9万
查看次数

标签 统计

asp.net-mvc ×1

c# ×1

ef-migrations ×1

entity-framework ×1