Update Model Function In MVC

Overview

In this article, we will see UpdateModel function in MVC. We will take the same example which we used in the previous article. Kindly refer to my previous article.

Introduction

Let’s quickly recap, what we had done in the previous article

MVC

At the moment, if I submit this form by clicking on create button, this form data will be posted to the Server and the parameters, shown in the screenshot, given below will receive the data, which is posted.

MVC

Instead of passing the individual properties of an employee object, we are passing the parameters to it, which creates control action method, where you can actually pass the employee object as a parameter.

Hence, this employee object is going to receive the posted data, as we are passing the employee object. We can get rid of the code, which we had written to pass the values, as given below.

MVC

One more thing, which we will mention here is there are a few valid modelation errors and we will make the use of IsValid property of model state object, as given below.

MVC

Hence, this property is a Boolean property, which means that it will return a true or a false. It returns true, if there are no validation errors, if there are any model validation errors, it will return false.

If it returns false, we need to provide a user an opportunity to correct those errors, retain in the same form and to do that, we are going to return the view. Hence, our code is given below.

MVC

Now, let’s run the solution. At the moment, in our table, I am having 11 rows, where we will create some data.

MVC

As you can see from the output below, a value known as Milind got posted and it is inserted successfully.

MVC

SQL output



Now, we can achieve the same thing without passing any parameter to our controller action method. Hence, get rid of the parameter and inside it, create a function i.e. create a new Employee object

MVC
Thus, we had created an Employee object and we had passed it to the UpdateModel function. Hence, this UpdateModel function is executing, as we are creating an employee object, where all the values will be null because it’s a new object and once the UpdateModel (employee) line is executed, the data we post will be bound to this employee object, which is the main purpose of this updateEmployeefunction.

Now, if you notice; there are two action methods- Create() which is of the same name and doesn't have a parameter. Now, if we want to overload, we have to differ either by a type or by number of parameters, but these functions don’t have the parameters.

When you build your project, we do get a compilation error.

MVC

EmployeeController already defines a member called Create with the same parameter types. Thus, one way to fix an error is to rename it,

MVC

Thus, we had renamed our controller and we had specified an ActionName, where we want our controller to respond in order to post a request.

Whenever we post some data, we want the form to insert the values and should respond to our request as HTTPPost and ActionName is Created.

For the sake of consistency, rename the other action method as Get.

MVC

Now, let’s run the app and let’s fill the form with some details, shown below.

MVC

As you can see from the output, shown above, I had filled the form with some details. Thus, when you click on create, you will be redirected to Index action method. Thus, our ActionName is working properly.

MVC

SQL output

MVC

Though our action names are different, we are specifying ActionName attribute here. These controllers' action methods are going to respond depending on whether it’s a GET or POST request.

Note

Right now, we didn’t get any error “Adding the specified count to the semaphore would cause it to exceed its maximum count”.

Restart the IIS or remove the connection pooling line in your web.config file.

Up Next
    Ebook Download
    View all
    Learn
    View all