Published on May 11, 2015 by Jamie Munro
You've added some data validation and you want to test it out in action. The data validation can be standard data annotations, custom data attributes, or data validation implemented via an IValidatableObject
Unit testing data validation can be accomplished by creating a ValidationContext
in conjunction with the Validator
helper class. To demonstrate how this is accomplished, I will use the ValidationModel
class I created in the Removing Data Validation from MVC Controllers
. As you may recall this class contained an IValidatableObject
that ensured if the boolean FixedQuantity
was true, the MaxQuantity
property must be null.
Read more >
Tags: validation | ASP.NET | mvc | Testing | unit testing | ivalidatableobject | validationcontext
Published on May 6, 2015 by Jamie Munro
You require custom data validation that cannot be accomplished via the built-in data annotation validation attributes - or - your data validation requires access to multiple properties in your model.
Many times the easy answer is to place the validation inside of your controller. However, I'm a strong believer of placing data validation outside of your controllers and within your data model. This is commonly accomplished by adding data annotation above the properties, such as the [Required] attribute. Another common way is to create your own validation attributes.
This example will use a third way and that is to implement the IValidatableObject interface. This interface defines a Validate function that must be implemented and because it is inside of your data model it has access to all properties within your model.
Read more >
Tags: validation | ASP.NET | mvc5 | ivalidatableobject | validationcontext
Published on May 4, 2015 by Jamie Munro
On a variety of actions inside your controllers you call the Automapper NuGet Package
to convert from one model to another and you do not want to continue repeating this code each time.
Applying an MVC result filter
to the action can execute the Automapper code after the action has been processed by the MVC framework and before the view is rendered.
Please note this is an excerpt from Chapter 9 of my ASP.NET MVC 5 with Bootstrap and Knockout.js
book. In this chapter I provide a brief overview of the 5 different MVC filters and then over the next two chapters provide detail examples of each. The following post is leveraging the example of a Result Filter.
Read more >
Tags: ASP.NET | mvc | automapper | mvc5
Published on Apr 29, 2015 by Jamie Munro
You've created a nice feature leveraging Knockout.js and now you want to re-use this feature on another page, another site, or in a slightly different fashion. This example will extend the previous (Un)Check All using #KnockoutJS
and make it easily re-usable.
A Knockout component
can be created using a mixture of HTML (with data bindings) and a Knockout ViewModel. By altering the previous check all example and making it slightly more re-usable, it can be easily added to other pages with minimal effort. If you are not already familiar with the example being extended, please take a minute and give the (Un)Check All using #KnockoutJS
a quick read.
Once the component is created, it can be included on any page with this simple HTML:
<checkall params="items: items, selectedItems: selectedItems"></checkall>
Read more >
Tags: KnockoutJS | knockoutjs | component
Published on Apr 27, 2015 by Jamie Munro
Radio buttons are hard-to-see, not easy to select, and let's face it, quite mundane. You would like to replace these radio buttons with a group of buttons that represent the same functionality, e.g. only one of the options may be selected at any given time.
Leveraging Bootstrap which provides many incredibly styled components for buttons, alert boxes, tables, forms, etc... regular radio buttons will be replaced by a button group (see screenshot below). Knockout.js will be used to create a custom data binding
that will make the group of buttons act like regular radio buttons (with a nicer look of course).
This example assumes you have a basic understanding of both Bootstrap and Knockout.js. If you don't, feel free to explore my latest book ASP.NET MVC 5 with Bootstrap and Knockout.js
for a great introduction, plus many more examples.
The versions used for this example are 3.3.4 for Bootstrap and 3.3 for Knockout.js. This example should be compatible with older versions of these frameworks.
Read more >
Tags: KnockoutJS | knockoutjs | Bootstrap | bootstrap | radio buttons | buttons