asp.net mvc tutorial for beginners
Lesson 89 of 496 · Part 16 Difference between updatemodel and tryupdatemodel · Database
Part 16 Difference between updatemodel and tryupdatemodel
Text version of the video
http://csharp-video-tutorials.blogspot.com/2013/05/part-16-difference-between-updatemodel.html
Slides
http://csharp-video-tutorials.blogspot.com/2013/09/part-16-difference-between-updatemodel.html
All ASP .NET MVC Text Articles
http://csharp-video-tutorials.blogspot.com/p/aspnet-mvc-tutorial-for-beginners.html
All ASP .NET MVC Slides
http://csharp-video-tutorials.blogspot.com/p/aspnet-mvc-slides.html
All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd
All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists
In this video we will discuss the differences between updatemodel and tryupdatemodel functions. Please watch Part 15, before proceeding.
Make changes to “Create_Post()” controller action method as shown below.
[HttpPost]
[ActionName(“Create”)]
public ActionResult Create_Post()
{
EmployeeBusinessLayer employeeBusinessLayer =
new EmployeeBusinessLayer();
Employee employee = new Employee();
UpdateModel(employee);
if (ModelState.IsValid)
{
employeeBusinessLayer.AddEmmployee(employee);
return RedirectToAction(“Index”);
}
else
{
return View();
}
}
Submit the page without entering any data. You will get an error stating “The model of type ‘BusinessLayer.Employee’ could not be updated”. This is because “DateOfBirth” property of “Employee” class is a non-nullable DateTime data type. DateTime is a value type, and needs to have value when we post the form. To make “DateOfBirth” optional change the data type to nullable DateTime as shown below.
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public DateTime? DateOfBirth { get; set; }
}
Submit the page without entering any data. You will now get a different error stating – Procedure or function ‘spAddEmployee’ expects parameter ‘@Name’, which was not supplied.
This is because, the parameters of stored procedure “spAddEmployee” are all required. Make all these parameters optional.
Submit the page without entering any data. You will now get a different error stating – Object cannot be cast from DBNull to other types.
To fix this error, make changes to “Employees” property in “EmployeeBusinessLayer.cs” file as shown below. Notice that we are populating “DateOfBirth” property of “Employee” object only if “DateOfBirth” column value is not “DBNull”.
if (!(rdr[“DateOfBirth”] is DBNull))
{
employee.DateOfBirth = Convert.ToDateTime(rdr[“DateOfBirth”]);
}
Submit the page without entering any data. Notice that a blank employee row is inserted into tblEmployee table.
Now let’s make the following properties of “Employee” class required.
Name
City
DateOfBirth
To achieve this we can use “Required” attribute that is present in System.ComponentModel.DataAnnotations namespace. To use this namespace, BusinessLayer project need a reference to “EntityFramework” assembly. The changes to the “Employee” class are shown below.
namespace BusinessLayer
{
public class Employee
{
public int ID { get; set; }
[Required]
public string Name { get; set; }
public string Gender { get; set; }
[Required]
public string City { get; set; }
[Required]
public DateTime? DateOfBirth { get; set; }
}
}
Submit the page without entering any data. We now get an error stating – The model of type ‘BusinessLayer.Employee’ could not be updated. Notice that this error is thrown when UpdateModel() function is invoked.
Now let’s use TryUpdateModel() instead of UpdateModel(). Make changes to “Create_Post()” controller action method in “EmployeeController”.
Submit the page without entering any data. Notice that, we don’t get an exception now and the user remains on “Create” view and the validation errors are displayed to the user.
So, the difference is UpdateModel() throws an exception if validation fails, where as TryUpdateModel() will never throw an exception. The similarity is, both the functions are used to update the Model with the Form values and perform the validations.
Is it mandatory to use “UpdateModel()” or “Try”UpdateModel()” function to update the Model?
The answer is NO.
So the next question is, Why do we need to explicitly invoke model binding?
If you want to limit on what can be bound, explicitly invoking model binding can be very useful. We will discuss more about this in a later video session.