A MongoDB Tutorial using C# and ASP.NET MVC

11

Posted by Joe | Posted in ASP.NET, C#, MongoDB, MVC | Posted on 02-10-2011

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 the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

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

0

Posted by Joe | Posted in Ajax, C#, MVC | Posted on 24-07-2011

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 the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Model Binding MongoDB ObjectId with ASP.NET MVC

4

Posted by Joe | Posted in ASP.NET, C#, MongoDB, MVC | Posted on 12-06-2011

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 the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

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

0

Posted by Joe | Posted in ASP.NET, C#, IoC, MVC | Posted on 09-06-2011

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 the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

Editing and binding nested lists with ASP.NET MVC 2

12

Posted by Joe | Posted in Ajax, ASP.NET, C#, JavaScript, jQuery, MVC | Posted on 06-06-2011

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 the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

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

18

Posted by Joe | Posted in Ajax, ASP.NET, C#, jQuery, MVC | Posted on 30-05-2011

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 the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

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

1

Posted by Joe | Posted in C#, MVC | Posted on 19-08-2010

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 the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

ASP.NET MVC Textbox with characters remaining HtmlHelper extension method

3

Posted by Joe | Posted in C#, JavaScript, MVC | Posted on 14-08-2010

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 the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

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

28

Posted by Joe | Posted in MVC | Posted on 17-02-2010

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 the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit

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

1

Posted by Joe | Posted in Ajax, jQuery, MVC | Posted on 16-02-2010

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 the rest of this entry »

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit