0

Ints/doubles in my app convert themselves to "0" if no value is entered by the user in the view. To stop this from happening I use "= null" in my model.

Eg.

public double? Distance { get; set; } = null;

Is this considered an OK thing to do or is there a better way? Thanks!

Question is different from the one suggested as I'm specifically dealing with null values in the model.

Pikapops
  • 631
  • 2
  • 8
  • 22

4 Answers4

2

This will do already:

public double? Distance { get; set; }
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
1

Creating a nullable model is enough.

public double? Distance { get; set; }

1

You can use nullable. By using '?' after the datatype you can make it nullable.

public double? Distance { get; set; }
Kate Fernando
  • 381
  • 1
  • 4
  • 18
0

the best way is to set the value to null where you are creating the object of model instead of assigning null initially .`

YourClass objectName = new Yourclass();

objectName.Distance = null;

may be i am wrong but when you initialize an object if it has nullable prop it is taken as null initially unless you dont assign a value

RAHUL S R
  • 1,569
  • 1
  • 11
  • 20