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 navigation with automatic active class for current page usng ASP.NET MVC

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

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.

Read more

Optimistic Concurrency with MongoDB C# driver and ASP.NET MVC – Prevent multiple users updating the same record

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

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.

Read more

Using KnockoutJS with SignalR in ASP.NET MVC

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

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 simple binding format, and when the underlying model is updated the UI is automatically updated to reflect the change.

SignalR is a library from Microsoft utilising HTML5′s WebSockets allowing server side code to call JavaScript functions on the client to create real-time functionality.

To me these two libraries seem like a match made in heaven. SignalR calling a JavaScript function to alter data, and Knockout automatically updating the UI, awesome!

In this post I’ll show how you can use KnockoutJS and SignalR together to create real-time web application with little effort. I’m going to create a very basic example of a web page showing a list of exchange rates hooked up using KnockoutJS. I’ll then create a server side page to update the data and use SignalR to call a JavaScript function to update the JavaScript model.

Read more

A new look!

Posted on by Joe in Personal | Leave a comment

Well I’ve had my blog for nearly 4 years and theme I was running was a bit behind the times. I spent some time today updating to a new responsive theme which I think looks much better!

Twitter Bootstrap validation styles with ASP.NET MVC

Posted on by Joe in ASP.NET, C#, jQuery, MVC | 3 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

MongoDB Sorting and Indexes

Posted on by Joe in MongoDB | Leave a comment

I’ve been looking at some of my MongoDB queries and trying to optimise them. I had one query that did a few ‘in’ statement and then sorted on a field called CreatedTime in descending order to give me the latest documents first.

My index looked something like:

{ “Field1″ : 1, “Field2″ : 1, “Field3″ : 1, “CreatedTime” -1 }

With this index I got the following information from the Explain method:

“nscanned” : 8120, “nscannedObjects” : 4364, “n” : 121, “scanAndOrder” : true, “millis” : 62

I couldn’t really work out why it was scanning my entire index, and also half the documents in the collection. Playing around with the query I found that it was the sorting that was causing it. After removing my sort I get the following result:

“nscanned” : 128, “nscannedObjects” : 126, “n” : 121, “millis” : 2

Much better, although unfortunately in the incorrect order. If I was using a capped collection I could use reverse natural order, but I’m not. I thought that as I had CreatedTime indexed descending, doing a sort on that field descending would be fine, but just for fun I thought I’d put the field as the first field specified in my index to see if that would alter how MongoDB stores and uses the index:

{ “CreatedTime” -1, “Field1″ : 1, “Field2″ : 1, “Field3″ : 1 }

Now running the query gives me the following:

“nscanned” : 256, “nscannedObjects” : 121, “n” : 121, “millis” : 2

That’s more like it, and you see the scanAndOrder attribute is no longer there. I’m limiting the query to 121 results, and the number of documents hit is 121, which is good as that means the query itself only used the index, and the exact documents were only accessed to get the fields I’m returning.

The query is scanning 256 index entries, presumably that how many it had to look at to match 121 documents based on my other query criteria.

A MongoDB Tutorial using C# and ASP.NET MVC

Posted on by Joe in ASP.NET, C#, MongoDB, MVC | 35 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 | 1 Comment

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 | 11 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