How can I make my string property nullable?

前端 未结 9 998
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-14 00:08

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

相关标签:
9条回答
  • 2020-12-14 00:28

    You don't need to do anything, the Model Binding will pass null to property without any problem.

    0 讨论(0)
  • 2020-12-14 00:33

    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.

    0 讨论(0)
  • 2020-12-14 00:38

    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.

    0 讨论(0)
提交回复
热议问题