How to embed Javascript into an ASP.NET MVC application

Recently I’ve been working on an application that provided a generic framework for entering data into a form. We chose the framework approach for two reasons:

  1. It was faster to build new forms when we only had to specify the fields and validation rules. All of the control logic and data persistence was already in the framework.
  2. Form quality improved because the framework made it easier to validate fields which encouraged developers to validate input properly.

While the framework was great for simple forms it just didn’t work when complex business logic needed to be applied. Instead of throwing away the framework we used Javascript hooks that were called at various points in the process to allow the form to customize the business logic.

Continue reading

Posted in Models | Leave a comment

ASP.NET MVC Paging

We’ve all seen the example “list” actions that return every row. Sure they’re easy to code but the in real world how often can you get away with that? Most of the time you will want to use paging in your results so the user only sees a limited number of results at a time. Luckily there’s a NuGet package for that. It’s called PagedList.Mvc and it uses the PagedList by the same author.

Continue reading

Posted in General | Leave a comment

Organizing your source with regions

When writing large applications its important to spend time making sure your source code is clean. My preferred approach to source code management is to have one file for every class/interface. With ASP.NET MVC typically that results in a lot a files for the models Often I find that our business logic is contained in single service class. Inside that class we have methods that can be logically grouped together.

Continue reading

Posted in General | Leave a comment

Custom ASP.NET MVC Membership Provider

ASP.NET MVC has an easy to use built-in system to handle authentication and authorization that’s based on the existing ASP.NET membership and role providers. Restricting access to a controller or action is as simple as adding an attribute. You can use any existing membership or role provider but if you’re building a website writing your a customer authentication provider may provide you with a better fit as it allows you to integrate with your own schema.

Continue reading

Posted in General | Leave a comment

ASP.NET MVC Editor Templates

You may have heard about that ASP.NET MVC editor templates. They’re a feature added in ASP.NET MVC 2. But what are they and why would you use them?

One of the really cool features of ASP.NET MVC is that it can automatically work out how to display model. It does this by rendering all of the properties using a default template. Luckily the smart people at Microsoft provided a way to override the default template. The even allow you to specify one template for displaying (Display Template) and a different one for editing (Editor template).

Continue reading

Posted in Views | Leave a comment

Using [ScriptIgnore] to hide public properties in a JSON result

Recently I needed write an action to return a model in JSON format. Serializing a model into JSON format using ASP.NET MVC is as simple as using the Json() method instead of the View() method. The problem was that I didn’t want all of the public properties of the model to be return. Here’s a simple version of the controller to give you an idea of what it looked like:

Continue reading

Posted in Atttributes | Leave a comment

ASP.NET MVC DropDown List Example

Today I wanted to cover implementing a dropdown list with ASP.NET MVC. I’ll use the same example I used when I blogged about implementing select lists except that this time you can only choose one category.

Continue reading

Posted in General | Leave a comment

Sorting lists in the browser using jQuery UI Sortable

This post is based on one that I originally made on personal blog.

Recently I wanted to create a user interface where people could re-order lists of items and move items from one list to another. The interface had to be something special so I wanted people to be able to do this by dragging items in their web browser. To do this I started with an unordered list where each list item was another unordered list:

Continue reading

Posted in jQuery | Leave a comment

ASP.NET MVC Mini Profiler

Haven’t heard of the ASP.NET MVC Mini Profiler? It’s a tool developed by Stack Overflow to help them profile their site. Today they announced that it’s now available as a NuGet package.

Posted in NuGet | Leave a comment

Displaying messages from one action on the view of another

After my last post about why you should use the POST, redirect, GET design pattern I got an email asking how you can display a message from one action on the view returned by another. This person wanted to display a message on the view returned by the GET action indicating that the POST action was successful.

Continue reading

Posted in Controllers | Leave a comment