Type Safe SelectList Factory

I think that ASP.NET MVC is a huge step forward from ASP.NET Web Forms. Still, there are some parts of it that are disappointing. One is the SelectList constructor, that doesn’t use generics and has string parameters for field selection. To create a SelectList from an IEnumerable<> we would do something like this.

IEnumerable<Person> people = GetPeople();
SelectList selectList = new SelectList(people, "Id", "FullName");

This is again one of those places where the compiler won’t be able to help us. Writing member names as strings in the code is just horrible. To remedy this I wrote a factory method with those strings changed into lambdas.

IEnumerable<Person> = GetPeople();
SelectList selectList = 
  people.ToSelectList(people, p => p.Id, p => p.FullName);

jQuery UI Replacement for alert()

The Javascript alert(message) is a simple one-liner to show a message on a web page. It uses a standard system dialog box to display the message. On a site where most other UI elements are built using jQuery UI it is more natural to display the alerts in the same style. Unfortunately there is no built in one-liner for displaying a simple alert in jQuery UI, but it is easy to add an own one.

This is a standard jQuery UI dialog displayed using one line of code. It follows the same theme as the rest of the jQuery UI widgets used on the site and it behaves like a normal modal dialog. The code used for displaying the dialog is $.alert("message", "title"), which is a jQuery extension I added with a few lines of code.

Catch Errors in ASP.NET MVC Views

The compiler is my friend. It helps me check for simple spelling mistakes and alerts me when a change in one part of the code base breaks a reference somewhere else.

When developing in ASP.NET Web Forms I have had a hard time with the lack of  compilation of aspx pages. As long as the code is in the code behind,  errors are catched during compilation. If the code is in the aspx file itself (e.g. data binding code) it doesn’t fail until the page is accessed. A large application with hundreds of pages requires detailed testing to make sure all pages have been accessed.

In ASP.NET MVC (which I really recommend instead of Web Forms) there is a far better way. The views can be included in the project build, catching any simple mistakes when building. Unfortunately it is not a standard setting, but it is easy to add.

Trying jQuery UI

The best way to learn a new technology is to try it out in a small sample project. With web development being my weak spot I decided to give jQuery UI a try. The only thing missing was the objectives of the sample project. I decided to try to make a small game for my 5 year old daughter. This also gave an opportunity to introduce my wife into software design and development. We wanted a game that doesn’t require reading, is free from ads and has no time restrictions.

The result is a simple math game.

Software Development is a Job – Coding is a Passion

I'm Anders Abel, an independent systems architect and developer in Stockholm, Sweden.

profile for Anders Abel at Stack Overflow, Q&A for professional and enthusiast programmers

Code for most posts is available on my GitHub account.

Popular Posts



Powered by WordPress with the Passion for Coding theme.