Asp.net MVC, Html.DropDownList and Selected Value

June 18, 2009 14:04 by bryan

I recently ran into the altogether common problem of the Html.DropDownList helper rendering a drop down list with no value selected. This is a major problem when editing data as by default, the first value is selected and saving would mean the first value is used.

There have been a few issues resulting in the same error. My issue was that I was setting the Name of the drop down list to be equal to the property on my model. I was using the Entity Framework, and had an Image class with a navigation property called Category. I was using this to render the ddl:

<%= Html.DropDownList("Category", (IEnumerable<SelectListItem>)ViewData["categories"])%>

In my controller, I was setting the ViewData like this:

this.ViewData["categories"] = new SelectList(db.CategorySet.ToList(), "CategoryId", "Title", img.CategoryReference.EntityKey);

Unfortunately, even though I had set the selected value (third parameter to the SelectList constructor), the ddl had no value selected.

The fix was quite simple:

<%= Html.DropDownList("CategoryId", (IEnumerable<SelectListItem>)ViewData["categories"])%>

I just changed the Name of the drop down and handled the assignment in the controller.

The reason behind this problem is that asp.net MVC first looks for a match between the name of the drop down and a property on the model. If there’s a match, the selected value of the SelectList is overridden. Changing the name of the drop down is all it takes to remedy the issue.

 


Building up an MVC application

June 10, 2009 11:35 by bryan

When you open up MVC out of the box, you get the basic configuration, after a little playing around you soon see areas that require enhancing to, not only make your application more flexable, but also easier to maintain.

Here are a number of additional tasks you can perform that will help with your application.

Which IoC to use?

Don't worry, if you use the Common Service Locator it provides an adstraction over the IoC

How to validate?

I have already covered this one in a previous post, but I'd still go with xVal

Need to generate Themes?

I think it is so important to start correctly and generating the User Interface should be configurable, the easiest way of doing this is to implement a Theme, and Chris Pietschmann has do this for us, Implement Theme Folders using a Custom ViewEngine

Here is a copy of the source for safe keeping.

ASPNETMVC_Preview5_CustomThemeImplementation.zip (226.05 kb)

Need Ajax to work in MVC

Ajax.BeginForm and PartialViews

Extend the UrlHelper

Placing all your assets in one place is not only a good idea, but it also means you can be more flexable if you want to programmically change the theme of your user interface.

 

 

Which can be used like this:

Using Strongtypes and keep away from Strings

It appears so easy with MVC to use String everywhere, just DON'T, generate an extension for the UrlHelper

Now in your view it will look like this

Testing

I've been hit before by Microsofts in build Testing within Visual Studio 2008, great to have this feature built in, BUT, and a big but in order to run the tests you need to have Visual Studio install, so if you try and setup a Continuous Integration server you will also be required to install Visual Studio.

So best to go with what works well and that is nUnit, simple and easy, and more importantly loosely coupled

Mocking

With testing comes Mocking, and which framework do you use?  I also always say keep it simple and easy, so I'm going with Moq, short for "Mock-You", as this is the only Mock Framework that is built around .Net 3.5 and LINQ.

 

 


MVC Validation

June 10, 2009 10:15 by bryan

Validation is such an important part of development, and the best way I have found for the MVC is by Steven Sanderson, xVal, which is a validation framework for ASP MVC, even Scott Gu thinks it is absolutely fantastic.

If you are after using xVal with Linq to SQL then it is well worth checking out Integrating xVal Validation with Linq-to-Sql by Adam Schroder

It's worth checking out Client-side form validation made easy by Adrian Grigore to extend this validation even further.

Don't take my word for is, check out, ASP.NET MVC Validation Refresh: Best Techniques & Frameworks by Graham O’Neale


Videos for MVC

May 4, 2009 13:42 by bryan

Need a quick over view of how MVC can help you, take a look at these series of videos by Bill Burrows, all very simple and easy to follow videos, and what's more he is running the demos in VB.NET

The Model-View-Controller Framework

Dime Casts has a good selection of videos to watch


MVC LifeCycle

May 3, 2009 07:59 by bryan

To understand the the power of MVC you need to understand the Life Cycle, I could go in to great detail about the ASP.NET Lifecycle, but I have found a number of very good articles already providing good amount of information on the LifeCycle.  Before going to the pages here is a simple flow diagram of the steps the MVC process goes through

The ASP.NET MVC Pipeline - Steve Sanderson

ASP.NET MVC - Living in a Web Forms World


MVC has been released

March 18, 2009 14:12 by bryan

Finally Microsoft have released MVC...

 

ASP.NET MVC 1.0 provides a new Model-View-Controller (MVC) framework on top of the existing ASP.NET 3.5 runtime. This means that developers can take advantage of the MVC design patterns to create their Web Applications which includes the ability to achieve and maintain a clear separation of concerns (the UI or view from the business and application logic and backend data), as well as facilitate test driven development (TDD). The ASP.NET MVC framework defines a specific pattern to the Web Application folder structure and provides a controller base-class to handle and process requests for “actions”. Developers can take advantage of the specific Visual Studio 2008 MVC templates within this release to create their Web applications, which includes the ability to select a specific Unit Test structure to accompany their Web Application development.

The MVC framework is fully extensible at all points, allowing developers to create sophisticated structures that meet their needs, including for example Dependency Injection (DI) techniques, new view rendering engines or specialized controllers.

As the ASP.NET MVC framework is built on ASP.NET 3.5, developers can take advantage of many existing ASP.NET 3.5 features, such as localization, authorization, Profile etc.

If you have not already done so, check out Steve Sanderson's book on MVC

 


MVC Release Candidate 1

January 28, 2009 08:40 by bryan

Microsoft have release MVC Release Candidate 1, I've had a little play with MVC and it looks very promising, although it is a different way of programming for your web site, the benefits are huge.

You can download MVC Release Candidate 1 from this link

I'm personally going to wait for our usergroup member to publish his book before I take a leap in to the MVC world.


StructureMap Example

January 22, 2009 11:47 by bryan

After using StructureMap for a while I thought it would be nice to generate a simple example of how it all works, so I've built in Visual Studio 2008, both C# and a VB.NET version.

StructreMap Example.zip (4.47 kb)

StructureMapExampleVB.zip (9.41 kb)

For more information on StructureMap please go to their main website

If you need to know more about IoC's then it's worth making a look at the following links:

Inversion of Control

List of .NET Dependency Injection Containers (IOC)

IoC Benchmarks

MVC Storefront: Dependency Injection

NinJect


ASP.NET MVC Beta Released

October 17, 2008 07:23 by bryan

Microsoft have release for the Beta version of MVC (Model View Control), it has been around in the community preview release for some time and now it is in it's final stages before being fully released.

ASP.NET MVC 

Scott Gu's Release Notes

Free ASP.NET MVC “NerdDinner”

Scott Hanselmann on MVC

You need to make sure you are running Visual Studio 2008 before you even think about using MVC.

It's now time to take MVC as a serious method of development, as it won't be very long before the final offical release is shipped.