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 »
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 »
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 »
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 »
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 »
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 »
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 »
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 »
Posted by Joe | Posted in Ajax, ASP.NET, JavaScript | Posted on 29-06-2010
JSON doesn’t have a standard way to represent a date. You can read about the reasons behind this here.
If you are using an ASMX web service returning JSON then you’ll find it serializes the DateTime object to a string that looks like this:
/Date(1278943200000)/
The numer in this string is the number of milliseconds since January 1st 1970 UTC, and this number can be used as a constructor argument to the JavaScript Date object. So all we need to do is extract the number of milliseconds from the string. This can be done easily using the regular expression shown in the post linked above.
var dateString = "/Date(1278943200000)/";
var date = new Date(parseInt(dateString.replace(/\/Date\((\d+)\)\//, '$1')));
In the above example I’ve manually set the date string, but you may get this from an AJAX response from the ASMX web service. I then create a new Date object by using the regular expression and parsing the result at an integer.
Posted by Joe | Posted in ASP.NET, C# | Posted on 16-04-2010
Download source
When creating web applications with ASP.NET developers will usually create User Controls or Custom Server Controls. User Controls have the ASCX extension and allow developers to group together ASP.NET controls and functionality into a reusable control. Custom Controls are a bit more complex and require the developer to define the html that the control generates as well as all it’s functionality. It is also possible to add child ASP.NET controls to a Server Control and have the control render those child control and maintain their viewstate with little work.
In .NET 2.0 the abstract CompositeControl class was introduced, which is derived from WebControl but also uses the interfaces INamingContainer, which makes sure all child controls have a unique ID, and ICompositeControlDesignerAccessor which is used to allow the control to render the control automatically at design time based on the child controls. The CompositeControl also ensures that child controls are always created which saves using the EnsureChildControls method that many Custom Control developers will be familiar with.
In this post I’ll explain how to create a simple Composite Custom Control and how to customise the HTML that it renders.
Read the rest of this entry »