Tuesday 13 December 2011

Automatically trim html controls in a asp.net mvc project

I have noticed over the years that users being users sometimes posts forms on a website that has leading or trailing spaces into the input controls. This I find is especially a problem when the user has copied and pasted an email/website address from a internet page or email. On the surface this might be OK but for me especially for email and website addresses when I display this back in a href then sometimes we get:-
- 'mailto:// email@email.com ' or
- href=' http://www.wildesoft.net '

So what I need to remember is to either a) trim before I send to the database or b) trim after I retrieve from the database. I for one prefer option one, better to keep the database correct rather than fixing output in the UI. But is there a way that we can do this for every html control that is posted? With asp.net MVC we can override the default model binder which performs a trim before the value gets to the action method on a controller.

All you need to do is to add the following class to your mvc project:-
public class TrimModelBinder : DefaultModelBinder
{
  protected override void SetProperty(
      ControllerContext controllerContext,
      ModelBindingContext bindingContext,
      PropertyDescriptor propertyDescriptor, object value)
  {
    if (propertyDescriptor.PropertyType == typeof(string))
    {
      var val = (string)value;
      if (!string.IsNullOrEmpty(val))
        val = stringValue.Trim();

      value = val;
    }

    base.SetProperty(controllerContext, bindingContext, 
        propertyDescriptor, value);
  }
}
Then in your application start up method:-
protected void Application_Start() {
  InitContainer();
  ...
  ModelBinders.Binders.DefaultBinder = new TrimModelBinder();
}
Now when a user keys in a leading or trailing space into any input control then the TrimModel binder kicks in and automatically removes it for you.

The only instance where this may be a problem is when a user has a leading or trailing space on their password.

Monday 5 December 2011

Simple.Data and mysql

To connect Simple.Data to a MySQl database is easiest done via Nuget. NuGet is a Visual Studio extension that makes it easy to install and update open source libraries and tools in Visual Studio.

As of 1st December 20111 the code has been updated to work with version Simple.Data 0.12.2.1

To install via Nuget goto your Package Manager Console window and type:-
PM> Install-Package Simple.Data.Mysql

After installation has been completed then you may need to also install the data connector. As of 1st December the latest version of the connector is 6.4.4

PM> Install-Package Mysql.Data

Please note: You may not need to perform this step as the version of you choice will be loaded dynamically at run time. This dynamic loading means that it's enough that the Mysql.Data.dll file is present in the same directory as Simple.Data.Mysql.Mysql40.dll at runtime. You don't have to take a dependency on the connector if you don't want to.

If all has succeeded then your solution will contain a packages.config file.
<?xml version="1.0" encoding="utf-8"?>
<packages>
 <package id="MySql.Data" version="6.4.4" />
 <package id="Simple.Data.Ado" version="0.12.2.1" />
 <package id="Simple.Data.Core" version="0.12.2.1" />
 <package id="Simple.Data.Mysql" version="0.12.2.1" />
</packages>

Now you are ready to start querying your database.

If you need any help then please direct your questions to the user group.

A big thank you to Vidar Sømme and Richard Hopton who has made all this possible.