Introduction

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.

		Nullable type using updatemodel in MVC
		Figure 1
		
		Nullable type using updatemodel in MVC 4.0
		Figure 2
		

Let’s move to the properties which we define in TechAltum class which is model for this example:-

	Nullable type using updatemodel in MVC 4.0
		Figure 3
		

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:-

		updatemodel output in mvc
		Figure 4
		

As you can see that I will simply submit the blank row with null values.

UpdateModel support the null value if no model validation will be implemented.

Note: - I will discuss the model validation in next article series. when we use Model validation using Required keyword then it will throw an exception and to get rid of this exception we will use TryUpdateModel and ModelState.
I will explain these two topics in my next article series.

Email Address

For any query you can mail me at info@techaltum.com