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

Twitter Bootstrap validation styles with ASP.NET MVC

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

Well it’s been a while since I’ve been blogging, two kids under 2 will do that to you. I’m going to try and get back into writing some regular posts.

Recently I’ve been playing around with Twitter Bootstrap, so the first thing I wanted to do was add it to an ASP.NET MVC 4 project. Getting the project set up was all pretty easy, and I quickly created a form using the horizontal form styles.

Read more

A MongoDB Tutorial using C# and ASP.NET MVC

Posted on by Joe in ASP.NET, C#, MongoDB, MVC | 54 Comments

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 have been working with it now for around 6 months on an enterprise application and so far am loving it. Our application is currently in alpha phase but should be public early next year! If you are used to working with an RDBMS, it takes a little bit of getting used to as generally you work with a denormalized schema. This means thinking about things quite differently to how you would previously; you’re going to have repeating data which is a no-no in a relational database, but it’s going to give you awesome performance, sure you may need an offline process that runs nightly and goes and cleans up your data, but for the real time performance gains it’s worth it.

Read more

ASP.NET MVC 2 client side validation for dynamic fields added with ajax

Posted on by Joe in Ajax, C#, MVC | 2 Comments

It’s quite common to add fields to a form dynamically using ajax, for example you may have a list of phone numbers for a user, and they could have many different phone numbers.

Using MVC you can easily add fields to a form by making an ajax call to an action that returns the rendered result of a partial view, but if you’ve tried doing this when using client side validation you’ll find the client side validation doesn’t work for those fields dynamically added to the form via the ajax call.

In this article I’ll explain a simple way to enable client side validation on those dynamically added fields.

Read more

Extract content from HTML and split words into an array using C#

Posted on by Joe in C# | 3 Comments

The other day I was messing around with Full Text Search with MongoDB (which I’ll probably write about in the future), and wanted to take some HTML content and remove all the HTML tags, extract the actual content, then split up the words into an array.

I wrote the following extension method that seems to do the trick. I’ve not tested it too much so there are likely some scenarios where it won’t work, but for those I’ve tried it’s worked fine.

using System.Text.RegularExpressions;
using System.Web;

public static string[] Tokenize(this string value)
{
    //Remove Html tags
    value = Regex.Replace(value, @"<.*?>", string.Empty);

    //Decode Html characters
    value = HttpUtility.HtmlDecode(value);

    //Remove everything but letters, numbers and whitespace characters
    value = Regex.Replace(value, @"[^\w\s]", string.Empty);

    //Remove multiple whitespace characters
    value = Regex.Replace(value, @"\s+", " ");

    //Trim, set to lower case and split to array
    return value.Trim().ToLower().Split(' ');
}

Model Binding MongoDB ObjectId with ASP.NET MVC

Posted on by Joe in ASP.NET, C#, MongoDB, MVC | 13 Comments

If you’re using the MongoDB C# driver with ASP.NET MVC and have a property of type ObjectId in your model you will get the following error when trying to bind back to the model.

The parameter conversion from type ‘System.String’ to type ‘MongoDB.Bson.ObjectId’ failed because no type converter can convert between these types.

This can easily be resolved by creating a custom Model Binder which I’ll explain how to do in this article.

Read more

Creating strongly typed wrappers around untyped dictionaries in ASP.NET using the Castle DictionaryAdapter

Posted on by Joe in ASP.NET, C#, IoC, MVC | Leave a comment

Often when writing web applications you find yourself writing strongly typed wrapper classes around untyped dictionaries, such as Session, QueryString or even Web.Config Application Settings.

This is quite a tedious task, but using the Castle DictionaryAdapter this is all wrapped up nicely, and all you need to do is create an interface.

Read more

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# | 3 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 | 39 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

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