Question

I have a numeric property on my model and i am using editorfor on my razor view with it. The field is not mandatory but the default validation makes the user enter a value because it wont accept an empty string for a number. I have ended up changing the model property to a string and then putting my own custom validation attribute on the property. This cant possibly be the correct way of getting what i want....can it??

[NonMandatoryDoubleValidation("Latitude")]
    public string Latitude { get; set; }
Was it helpful?

Solution

What you need is a nullable double: double?. That way your variable will accept empty string or null value as well as double values. However, you'll need to check if it's empty each time you use it with Latitude.HasValue and use Latitude.Value to get its value.

OTHER TIPS

How about a nullable double:

[Required]
public double? Latitude { get; set; }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top