Concept of Nullable type using UpdateModel in MVC


In my last article I discussed how we can fetch data from view to controller using UpdateModel in MVC. In last example we use UpdateModel method where we pass model object i.e. your techaltum class. I am taking the same example in this article.

For Example

We have seen that when we click on button our data posted in model and using this model we insert this data into database.
Now I am again executing this example and this time I am not filling any data and submitting the form.

Let’s move to the properties which we define in TechAltum class which is model for this example:-
As you can see that fees and DOJ is type of int and DateTime respectively and both are struct type. And we know that struct do not support the null value. Let’s make it nullable type and again execute the code.

Nullable Code

 public int? fees { get; set; }
    public DateTime? DOJ { get; set; } 
by putting the ? with datatype these variable become nullable type and now they can accept null values.
It will execute the code successfully. For cross check, check at your database:-

No comments:

Post a Comment