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
You don't need to do anything, the Model Binding
will pass null
to property without any problem.
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.
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.