Using KnockoutJS with SignalR in ASP.NET MVC

KnockoutJS is a MVVM implementation for JavaScript written by Steve Sanderson, in my opinion the author of the best ASP.NET MVC textbooks available. Simply put it lets you bind a JavaScript object model to your HTML UI using a Read more

A MongoDB Tutorial using C# and ASP.NET MVC

In this post I'm going to create a simple ASP.NET MVC website for a simple blog that uses MongoDB and the offical 10gen C# driver. MongoDB is no NOSQL database that stores information as Binary JSON (BSON) in documents. I Read more

Linq To SQL Tutorial

Check out some of my other Linq to SQL posts: EntityBase Inheritance Modifiers with SQLMetal Linq to SQL with WCF Services Linq to SQL Framework (Repository/Business wrapper) ObjectDataSource binding with server side paging and sorting Load Options Generic Framework using reflection This is a basic tutorial for Read more

MongoDB windows service won’t start

Posted on by Joe in MongoDB | Leave a comment

A couple of times now my MongoDB windows service won’t start after rebooting my machine. If you look at it in the services list it flicks between Starting and Started states but never really startsm and there doesn’t seem to be a way to get it to stop.

To fix this delete the mongod.lock file in your MongoDB data directory. It will immediately create a new one, but then the service will start.

Editing and binding nested lists with ASP.NET MVC 2

Posted on by Joe in Ajax, ASP.NET, C#, JavaScript, jQuery, MVC | 22 Comments

Dynamically editing lists of data and binding back to the model with MVC is a little complicated as the id’s of the form elements need to all tie up for binding to succeed. Recently I had a model, which contained a list of an object, which in turn contained another nested list. Getting this to easily bind back to the model when adding to the lists dynamically was a bit of a headache so I’ll explain how I did it.

This article is inspired by this article by Steve Sanderson, but I also explain how to adapt it to bind nested lists.

Read more

How to register and get all instances that implement an interface using Autofac

Posted on by Joe in C# | 2 Comments

I’ve just started using Autofac, where I’ve previously used Ninject and StructureMap.

UPDATE (09/06/2001): Although this post is about registering all instances that implement a specific interface, my reason for doing so was to create a bootstrapper task. I’ve recently found AutoFac has an IStartable interface which basically does the same thing.

I have an interface called IBootStrapperTask that looks like this:

public interface IBootStrapperTask
{
    void Execute();
}

I then have several tasks that implement this interface, which I want to get and run when my application starts. First I need to register all types that use this interface, which I don’t want to do by automatically registering all types. I can do this like so:

var builder = new ContainerBuilder();

builder.RegisterAssemblyTypes(Assembly.Load("MyAssesmbly")).Where(t => typeof(IBootStrapperTask).IsAssignableFrom(t)).InstancePerLifetimeScope().AsImplementedInterfaces();

From what I can tell you can only do this on a per assembly basis. Now I have all my types registered I can get and execute them all:

var container = builder.Build();

var tasks = container.Resolve<IEnumerable<IBootStrapperTask>>();
foreach (var task in tasks)
{
    task.Execute();
}

ASP.NET MVC simple server-side ajax paging using jQuery

Posted on by Joe in Ajax, ASP.NET, C#, jQuery, MVC | 35 Comments

It’s always important to page your data on the server side so that you are only hitting the database to get the page you currently need, rather than getting all the results and paging on the client. In this post I’ll explain how to perform simple ajax paging on the server side using ASP.NET MVC 3.

Read more

Looking for .NET Developers at Community Engine on the Northern Beaches in Sydney

Posted on by Joe in Personal | 6 Comments

I haven’t blogged for a while as I’ve been really busy in my new role as Platform Lead at Community Engine where we are currently looking to expand our team.

We’re doing some awesome stuff here to create a cutting edge product platform that will host our future client websites.  The team is probably the best technically that I’ve worked with, and our implementation of SCRUM is the best I’ve seen. We’re also using all the latest and greatest technologies from Microsoft so it’s a great opportunity to get these skills and get them on your CV!

The product platform is a multi-tenant web application hosted in the cloud with Amazon EC2 written with ASP.NET MVC and .NET 4 and Entity Framework 4.  We’re also making use of MongoDB which is used by the likes of FourSquare and SourceForge, along with Solr for searching and distributed caching with Redis. Who wouldn’t want to work with this technology stack?

If you want to get involved you can apply through our careers page. Make sure you mention my blog when you do. We are also interested in hearing from candidates overseas.

We are looking for a Technical Lead and a number of mid/senior developers, as well as many other roles:

Using the jQuery UI Datepicker with Fancybox

Posted on by Joe in jQuery | 3 Comments

I just wasted what seems to be far too much of my life (about 30 minutes), trying to figure out why the jQuery UI Datapicker wouldn’t work when it was applied to a text box within a Fancybox.

It turns out it was working, but it was being placed behind the Fancybox which had it’s z-index set to 1101.

Adding the following CSS worked a treat

#ui-datepicker-div
{
    z-index: 1102 !important;
}

ASP.NET MVC authorize attribute using action parameters with the ActionFilterAttribute

Posted on by Joe in C#, MVC | 1 Comment

ASP.NET MVC provides the AuthorizeAttribute which ensures there is a logged in user. You can also provide parameters to restrict actions or controllers to only be accessible to certain roles or users. You can also create your own custom authorization attribute derived from AuthorizeAttribute to provide any custom authorization.

In addition to this general authorization you may want to restrict access based on the current user and a parameter from the action. For example, say you have an action method to edit the details of a product.  You would pass the ID of the product to the action method, and you may only want certain users to be able to edit this particular product.  The AuthorizeAttribute doesn’t allow you to do this but you can create your own attribute derived from ActionFilterAttribute which gives you the desired result.

Read more

Linq lambda expression IEqualityComparer for IEnumerable.Distinct and Except

Posted on by Joe in C#, Linq | 3 Comments

One of the things that annoys me with the IEnumerable.Distinct method is that it has no overload allowing you to give a lambda expression to specify a particular property to perform the distinction, you have to give an IEqualityComparer.

I did a quick Google search and found this post. The guy here provides the following LambdaComparer class:

Read more

ASP.NET MVC Textbox with characters remaining HtmlHelper extension method

Posted on by Joe in C#, JavaScript, MVC | 5 Comments

Here is a couple of HtmlHelper extension methods, CharactersRemainingTextBoxFor and CharactersRemainingTextAreaFor which render a textbox/textarea with a span tag that displays the number of characters remaining.  They also include the JavaScript to make this work, and to limit the number of characters entered in the textarea. The maximum limit comes from the model by using the StringLengthAttribute.

Download source

Read more

WordPress automatic upgrade timeout

Posted on by Joe in Personal | Leave a comment

I was just trying to upgrade my wordpress blog to version 3 using the automatic upgrade but it kept stopping after 30 seconds while unpacking the update. I checked on my server and it hadn’t completed the unpacking.  I loaded up the PHP.ini file and after a look around found the max_execution_time setting which was set to 30 seconds. I increased this and the update worked fine.