My model:
[DisplayName("Height")]
public int? Height { get; set; }
[DisplayName("Width")]
public int? Width { get; set; }
View:
<%= Html.TextBoxFor(x => x.Width) %>
<%= Html.TextBoxFor(x => x.Height) %>
Action:
if (ModelState.IsValid)
SaveSettings(model);
When empty strings are passed from view, the ModelState is false, but I need empty strings to be valid input, so that nulls are passed and ModelState.IsValid will be true. What can I add to the view to add this logic ? Or perhaps any other solution ? Thanks a lot for help.
There are work arounds, such as using a string property on your ViewModel and using a RegEx validator to make sure it is a number. The Regex shouldn’t fire on an empty input. Then when you go from ViewModel to Model you’ll have to do a conditional on the string value like