Skip to content

Greg Jopa

CRUD Operations w/ XML Data in ASP.NET MVC 3

ASP.NET2 min read

CRUD Operations with XML Data in ASP.NET MVC 3

Create, read, update, and delete (CRUD) operations are easy to perform in ASP.NET MVC. By using the default T4 scaffolding templates and strongly-typed views you can quickly build a web application that can create, update, and delete records.

Using ASP.NET MVC 3 I built a simple Billing Application that performs CRUD operations on an XML file. With LINQ I was able to quickly write code to update nodes in the XML file.

How my app works

My ASP.NET MVC 3 Billing App uses the Repository pattern for accessing data. The advantages with this pattern are it makes my methods easier to maintain and limits the amount of duplicated code in the data access layer. For each CRUD operation I have a method in my repository.

Model

I created a Billing.cs class based on the structure of my Billings.xml file.

In my Billing.cs class I used the System.ComponentModel.DataAnnotations namespace for adding validation to my model properties.

My model folder also contains my interface IBillingRepository.cs.

And all the heavy lifting is done by my BillingRepository.cs class.

Controller

I have one Controller named BillingController.cs which interacts with my repository and contains get and post requests for performing CRUD operations. By using the ModelState.AddModelError() method I am able to display Exception error messages in red font for any failed CRUD operations.

View

My Views are standard since they use the default T4 scaffolding templates (Create, Delete, Details, Edit, and List). One thing I added to my Create and Edit Views was the jQuery UI date picker. With ASP.NET MVC the Scripts and Content folders come with the jQuery UI base theme installed by default. To utilize jQuery UI I added an optional RenderSection to my layout master page in the head for adding the jQuery UI stylesheet to specific Views. All I had to do was add the jQuery UI stylesheet and JavaScript code to the Create and Edit Views to get the date picker up and running.

_Layout.cshtml optional RenderSection tag

Conclusion

ASP.NET MVC makes it easy to perform CRUD operations on data. The T4 Scaffolding templates create forms and lists for your strongly-typed Views so you don't have to waste time manually coding them yourself. These templates also provide form validation which speeds up the development process. ASP.NET MVC comes with some great tools to get your web apps up and running very quickly.