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

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

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

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

ASP.NET MVC – Using Controller.UpdateModel when using a ViewModel

Posted on by Joe in MVC | 31 Comments

When updating a model in MVC it is common to use the Controller.UpdateModel method.  I recently ran into an issue where I was using a custom ViewModel which meant that UpdateModel could not map the updated data back to my model object. The solution to this was a simple one, but not as obvious as it should be due to intellisense not picking up the method’s overloads when the generic type is inferred. 

Read more

Creating a Delete link with MVC using POST to avoid security issues

Posted on by Joe in Ajax, jQuery, MVC | 6 Comments

It is fairly common to have a list of records with a hyperlink to delete a record.  The problem here is that with MVC the hyperlink will use a GET request to delete a record.  This is a fairly big security issue as anybody can browse to the URL and delete a record from your system.  In this post I’ll explain how you can use the Ajax helpers to create a hyperlink that will call your delete method without using GET.

Read more