I’ve been playing around some more with Twitter Bootstrap and MVC and wanted to create a top level menu where the active class was added automatically to the item corresponding to the current page. Initially I had an action link inside the list item tag, but as the active class needs to be added to the list item so I decided to create an extension method to generate the entire item for me.
Over on my MongoDB C# Tutorial Richard asked how you would prevent multiple users from updating the same document. In this post I’m going to update the project in the tutorial to support this.
In my blog example this issue could occur when user 1 clicks to edit a post. While editing, user 2 also edits the same post. User 2 finishes their edit before user 1 and saves the post. User 1 continues editing and saves their changes. User 2′s changes are completely overwritten and neither user has any idea that it happened.
In this implementation, based on the above scenario I’m going to display an error to user 2 to tell them somebody else has updated to the post. They won’t be able to save their changes and it would be up to them to reload the post and make their changes again. Of course you could do something much more exciting, like show the two versions side by side and allow the user to merge the changes.
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.
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.
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.
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.
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(' ');
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.
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.
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.