Yet Another ASP.NET MVC CRUD Tutorial

I know that I have not posted much on MVC, mostly because I don’t use it on my daily life, but since I find it so interesting, and since it is gaining such popularity, I will be talking about it much more. This time, it’s about the most basic of scenarios: CRUD.

Although there are several ASP.NET MVC tutorials out there that cover ordinary CRUD operations, I couldn’t find any that would explain how we can have also AJAX, optimistic concurrency control and validation, using Entity Framework Code First, so I set out to write one! I won’t go into explaining what is MVC, Code First or optimistic concurrency control, or AJAX, I assume you are all familiar with these concepts by now.

Let’s consider an hypothetical use case, products. For simplicity, we only want to be able to either view a single product or edit this product.

One thing you’ll notice is, I am including both the ProductId and the RowVersion properties as hidden fields; they will come handy later or, so that we know what product and version we are editing. The other thing is the included JavaScript files: jQuery, jQuery UI and unobtrusive validations. Also, I am not using the Content extension method for translating relative URLs, because that way I would lose JavaScript intellisense for jQuery functions.

OK, so, at this moment, I want to add support for AJAX and optimistic concurrency control. So I write a controller method like this:

So, this method is only valid for HTTP POST requests (HttpPost), coming from AJAX (AjaxOnly, from MVC Futures), and from authenticated users (Authorize). It returns a JSON object, which is what you would normally use for AJAX requests, containing three properties:

Success: a boolean flag;

RowVersion: the current version of the ROWVERSION column as a Base-64 string;

ProductId: the inserted product id, as coming from the database.

If the product is new, it will be inserted into the database, and its primary key will be returned into the ProductId property. Success will be set to true;

If a DbUpdateConcurrencyException occurs, it means that the value in the RowVersion property does not match the current ROWVERSION column value on the database, so the record must have been modified between the time that the page was loaded and the time we attempted to save the product. In this case, the controller just gets the new value from the database and returns it in the JSON object; Success will be false.

Otherwise, it will be updated, and Success, ProductId and RowVersion will all have their values set accordingly.

So let’s see how we can react to these situations on the client side. Specifically, we want to deal with these situations:

The user is not logged in when the update/create request is made, perhaps the cookie expired;

The implementation of the onSuccess function first checks if the response contains a Success property, if not, the most likely cause is the request was redirected to the login page (using Forms Authentication), because it wasn’t authenticated, so we navigate there as well, keeping the reference to the current page. It then saves the current values of the ProductId and RowVersion properties to their respective hidden fields. They will be sent on each successive post and will be used in determining if the request is for adding a new product or to updating an existing one.

The only thing missing is the ability to insert a new product, after inserting/editing an existing one, which can be easily achieved using this snippet:

1 Comment

This is a very nice article on "CRUD Operations Using Entity Framework in ASP.NET MVC". I have find out some other articles link from which I have learnt "CURD operations using Ajax Model Dialog in ASP.NET MVC!". I think this is very helpful for developers like me.