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; }
有帮助吗?

解决方案

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.

其他提示

How about a nullable double:

[Required]
public double? Latitude { get; set; }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top