This article is part 4 of an X-Part series. If you have not been following the series, you can find the first article Here.
On Today’s Agenda …
Welcome to Part 4 of the Your First MVC Application series. The aim of this article is to introduce Paging within the application using a NuGet package and we will also allow users to search for a contact by name. Then I will discuss Concurrency, the methods available to prevent it and we will then implement some Concurrency Handling into the application.
Let’s start of by adding paging to the list of our Contact’s, which is held on the Contact Controller’s Index view. To do this, we need to add a NuGet packaged called PagedList, which brings with it the PagedList datatype, which is a collection of the declared type, similar to a list but with added methods to allow us to easily page our data.
Right click on your Project and select Manage NuGet Packages. In the Search Box of the popup windows (Top right corner) type “PagedList”. Install the package, as illustrated below;
This article is part 3 of an X-Part series. If you have not been following the series, you can find the first article Here.
Up to now we have created a single Model, Contact, and modelled it’s controller and views. We then spent time exploring the Controller & it’s actions and improving them. We also implemented a soft-delete method.
The plan for Part-3 is to introduce a second Model into our application. This Model is going to hold Types of contact we may possibly have within our contact manager. The user needs the ability to add, edit and remove these types, but only removing them if they don’t have associated contacts.
We will then introduce a method to view Contacts based upon the type of contact they are, or display all contacts.
This article is part 2 of an X-Part series. If you have not been following the series, you can find the first article Here.
In Part 1 of this series we finished with an application capable of basic CRUD operations on a code-first model which entity framework used to create a simple, one-table database. The last action we committed was to create a code-scaffolded controller and auto-generate it’s views, using the default MVC template.
Inside a Controller
We are going to pickup exactly were we left off and discuss the inner workings of controllers. Open your solution from Part 1 and navigate yourself into the Controllers Folder and open HomeController.cs. We will start by analysing the HomeController as this default controller is incredibly easy to understand.
I am assuming you are here because you want to learn how to create web applications using ASP.Net MVC4. If you’re not, you are in the wrong place.
I’m not joking, read the article title!
This is not a quick example. Well, it is. But it could be more complex and done in a better way, I will admit. This is designed as an article for beginners just setting out with ASP.Net MVC4. I will build on this application in each progressive application in order to show new techniques and concepts. This will never be an enterprise level application, but this series will teach you the footing you need to succeed within the wonderful world of MVC4.
XML was designed to store and transfer data in a way that is both human and machine readable. XML emphasises simplicity, usability and generic use across the internet, and can be find in many of the darkest corners of the internet. Microsoft Office utilises XML, as do Open Office and Libre Office. XML is also at the heart of XHTML, RSS, Atom and Soap.
From my point of view, XML is genius. The same point of view shared by a vast majority of developers, which can be seen by it’s global usage.
When a user navigates to anything within your application they are first dealt with by the Routing Mechanism which parses the URI and invokes the controller and action called – This defaults to the Index action of your HomeController. The result is dependent on the controller action called. What if we need to check something first?
ASP.Net MVC allows us to create custom Filters to pre or post-process on a controller action. Action Filters are custom attributes that allow us to easily declare behaviour prior to an action being called, or after it has been called. Once we have created our behaviour we simply call an attribute above the action (or controller) for which it is to be used for. An attribute placed above a controller will be used for every action within that controller, for example;
There is always a legitimate situation using entity framework and asp.net MVC when you require all relationships to be loaded using eager-load. But at the same time there are occurrences within the application where using eager-load is a waste of resources. To this end, this blog post will show you how to inject include statements using the Linq Aggregate method.
Client-Side Optimisation involves employing certain techniques within websites in order to improve their load time. The faster your pages load, the more responsive your website appears to be. The more responsive your website appears to be, the happier your visitors are. There aren’t many thing more frustrating than waiting 30 seconds for a website to load. In this day and age we have significant download speeds across the board, but you must remember that not all are as fast as they can be and with almost everybody having access to the internet we have many a shared connection. These are the situations we would like to improve.
Concurrency conflicts occur when we have a situation in which one of our users retrieves an entity from the database in order to edit it, but before their changes are persisted another user updates that entity and persists it first. By default, entity framework will overwrite any changes if concurrency conflicts occur, which is generally a bad thing. In some applications this can be considered acceptable, though in others a concurrency conflict is something we won’t ever worry about (Such as a personal website with only one user.)
Over the last few days I have been working on the development of my Personal Website. I’ll be honest and admit that there isn’t much to it. I’m using it as a project hub, so no matter what I do or where I do it the information will be available via my Personal Website.
I’ll contain details of all my projects from now on, but not past (There are too many to sift through and edit so I won’t be embarrassed by their release. I’m classing them all as learning experiences!). My work experiences and future plans will also be available, as well as another way to get in touch!
Be sure to check it out!