I want to make the Middle Name of person optional. I have been using C#.net code first approach. For integer data type its easy just by using "?" operator to make in nullable. I am looking for a way to make my sting variable nullable. I tried to search but could not find the way to make it nullable.
我想让人的中间名可选。我一直在使用C#.net代码的第一种方法。对于整数数据类型,只需使用“?”即可轻松实现运算符可以为空。我正在寻找一种让我的sting变量可以为空的方法。我试图搜索但找不到让它可以为空的方法。
Below is my code. Please suggest me how to make it nullable.
以下是我的代码。请建议我如何让它可以为空。
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; }
}
7 个解决方案
#1
43
String is a reference type and always nullable, you don't need to do anything special. Specifying that a type is nullable is necessary only for value types.
String是一个引用类型,并且始终可以为空,您不需要做任何特殊操作。只有值类型才需要指定类型可为空。
#2
2
Strings are nullable in C# anyway because they are reference types. You can just use public string CMName { get; set; }
and you'll be able to set it to null.
无论如何,字符串在C#中都可以为空,因为它们是引用类型。你可以使用公共字符串CMName {get;组;并且你将能够将其设置为null。
#3
2
System.String is a reference type so you don't need to do anything like
System.String是一个引用类型,因此您不需要执行任何操作
Nullable<string>
It already has a null value (the null reference):
它已经有一个空值(空引用):
string x = null; // No problems here
#4
1
It's not possible to make reference types Nullable. Only value types can be used in a Nullable structure. Appending a question mark to a value type name makes it nullable. These two lines are the same:
无法使引用类型为Nullable。只有值类型可以在Nullable结构中使用。将问号添加到值类型名称使其可为空。这两行是相同的:
int? a = null;
Nullable<int> a = null;
#5
0
string
type is a reference type, therefore it is nullable by default. You can only use Nullable<T>
with value types.
string类型是引用类型,因此默认情况下它是可为空的。您只能将Nullable
public struct Nullable<T> where T : struct
Which means that whatever type is replaced for the generic parameter, it must be a value type.
这意味着无论为泛型参数替换何种类型,它都必须是值类型。
#6
0
As others have pointed out, string is always nullable in C#. I suspect you are asking the question because you are not able to leave the middle name as null or blank? I suspect the problem is with your validation attributes, most likely the RegEx. I'm not able to fully parse RegEx in my head but I think your RegEx insists on the first character being present. I could be wrong - RegEx is hard. In any case, try commenting out your validation attributes and see if it works, then add them back in one at a time.
正如其他人所指出的那样,字符串在C#中总是可以为空的。我怀疑你是在问这个问题因为你不能把中间名留空或空白?我怀疑问题在于您的验证属性,很可能是RegEx。我无法完全解析RegEx,但我认为你的RegEx坚持第一个角色出现。我错了 - RegEx很难。在任何情况下,尝试注释掉您的验证属性并查看它是否有效,然后一次一个地添加它们。
#7
0
You don't need to do nothing, the modelbinding will pass null to variable without problems.
您不需要做任何事情,modelbinding会将null传递给变量而不会出现问题。
#1
43
String is a reference type and always nullable, you don't need to do anything special. Specifying that a type is nullable is necessary only for value types.
String是一个引用类型,并且始终可以为空,您不需要做任何特殊操作。只有值类型才需要指定类型可为空。
#2
2
Strings are nullable in C# anyway because they are reference types. You can just use public string CMName { get; set; }
and you'll be able to set it to null.
无论如何,字符串在C#中都可以为空,因为它们是引用类型。你可以使用公共字符串CMName {get;组;并且你将能够将其设置为null。
#3
2
System.String is a reference type so you don't need to do anything like
System.String是一个引用类型,因此您不需要执行任何操作
Nullable<string>
It already has a null value (the null reference):
它已经有一个空值(空引用):
string x = null; // No problems here
#4
1
It's not possible to make reference types Nullable. Only value types can be used in a Nullable structure. Appending a question mark to a value type name makes it nullable. These two lines are the same:
无法使引用类型为Nullable。只有值类型可以在Nullable结构中使用。将问号添加到值类型名称使其可为空。这两行是相同的:
int? a = null;
Nullable<int> a = null;
#5
0
string
type is a reference type, therefore it is nullable by default. You can only use Nullable<T>
with value types.
string类型是引用类型,因此默认情况下它是可为空的。您只能将Nullable
public struct Nullable<T> where T : struct
Which means that whatever type is replaced for the generic parameter, it must be a value type.
这意味着无论为泛型参数替换何种类型,它都必须是值类型。
#6
0
As others have pointed out, string is always nullable in C#. I suspect you are asking the question because you are not able to leave the middle name as null or blank? I suspect the problem is with your validation attributes, most likely the RegEx. I'm not able to fully parse RegEx in my head but I think your RegEx insists on the first character being present. I could be wrong - RegEx is hard. In any case, try commenting out your validation attributes and see if it works, then add them back in one at a time.
正如其他人所指出的那样,字符串在C#中总是可以为空的。我怀疑你是在问这个问题因为你不能把中间名留空或空白?我怀疑问题在于您的验证属性,很可能是RegEx。我无法完全解析RegEx,但我认为你的RegEx坚持第一个角色出现。我错了 - RegEx很难。在任何情况下,尝试注释掉您的验证属性并查看它是否有效,然后一次一个地添加它们。
#7
0
You don't need to do nothing, the modelbinding will pass null to variable without problems.
您不需要做任何事情,modelbinding会将null传递给变量而不会出现问题。