Frage

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; }
War es hilfreich?

Lösung

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.

Andere Tipps

How about a nullable double:

[Required]
public double? Latitude { get; set; }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top