Need help learning MVC

by Donovan Brown 22. December 2010 19:58

Problem

I am struggling to learn MVC 2.

Solution

Get a copy of "Pro ASP.NET MVC 2 Framework, Second Edition". It is also offered in a Kindle version.

Review

Having had a lot of success with Web Forms and the data bound controls I really had a hard time when I first looked at MVC. I felt I was taking a step back in time to classic ASP.  The lack of all the creature comforts of Web Forms was hard for me to justify.  Also with the incredible suite of test tools from Microsoft Visual Studio Ultimate I was able to test my sites to ensure a high level of quality so I dismissed MVC about a year ago as a fad.

After the release of MVC 2 and hearing of plans for MVC 3 I realized it was not a fad.  I also work with companies all over the world and would ask teams their impressions of MVC to see what I was missing.  While at one client I was shown a very impressive Scrum Task Board written using MVC and was thinking to myself that would have been very difficult to do using Web Forms so I decided to give it a second look.

While searching for books to aid in my learning I ran across Pro ASP.NET MVC 2 Framework, Second Edition and have been very happy with the amount of great information in this book.  Once you get past his Web Form bashing it is a great book and I recommend it for anyone trying to learn MVC.

I must say that now I "get it" I am very fast a cranking out new functionality for a current project http://proTableSports.com. Now with MVC 2 and .NET 4.0 I don't feel I have lost like I did in the past.  I was able to use all the Membership, Role and Profile providers that I was used to in Web Forms.  With the addition of jQuery all my client side code is also easier to write.

I don't recommend books lightly.  I am certain you will enjoy this one.

Tags:

Work

MVC 2 Client Side validation messages are showing on page load

by Donovan Brown 19. December 2010 20:33

Problem

When I enable Client side Validation on my MVC 2 view the validation error messages are showing as soon as the page loads even if everything is valid.

Solution

Define the styles to hide content until an error is detected.

Code

/*----------------------------------------------------------
ASP.NET MVC FRAMEWORK DEFAULT CSS CLASS NAMES
----------------------------------------------------------*/
.error,.field-validation-error{color:red}

.input-validation-valid,.field-validation-valid,.validation-summary-valid{display:none}
.input-validation-error{background-color:#fee;border:1px solid red; outline: none}
.input-validation-error:focus{outline:none}
input[type="text"].input-validation-error:focus, select.input-validation-error:focus{outline:none}

.validation-summary-errors{color:red}
.validation-summary-errors span{font-weight:700}
.validation-summary-errors ul{list-style:disc inside}
.validation-summary-errors ul li{font-weight:normal}
.validation-summary-errors ul li label, .validation-summary-errors ul li span{display:inline !important; font-weight:normal}

Explanation

When you enable client side validation the content of the error messages and validation summary are rendered to the screen with a class of *-valid. The class is changed to *-errors when a validation error is detected.  If your style sheets does not hide the *-valid style sheet classes you will see that content when the page is display.

Tags: , ,

Work

My MVC 2 server side validation rules are not firing

by Donovan Brown 19. December 2010 19:54

Problem

I am using a Form Collection in my [HttpPost] action of my controller and my ModelState.IsValid is not being set correctly for invalid input.

Solution

Add a call to UpdateModel or TryUpdateModel which will populate your model and fire the validation rules setting the ModelState.IsValid property correctly.

Code

In the incorrect implementation below ModelState.IsValid is always true.

[HttpPost]
public ActionResult EditProfile(FormCollection values)
{
  
ProfileCommon profile = new ProfileCommon
();
   profile
= profile.GetProfile(User.Identity.
Name);
  
  
if(ModelState.
IsValid)
   {
      profile
.DisplayName =
values["DisplayName"];
      profile
.
Save();
     
return
RedirectToAction("Index", "Home");
   }
  
else
//Show validation errors
     
return
View();
}

In the correct implementation below ModelState.IsValid gets set by the call to TryUpdateModel.

[HttpPost]
public ActionResult EditProfile(FormCollection
values)
{
  
ProfileCommon profile = new ProfileCommon
();
   profile
= profile.GetProfile(User.Identity.
Name);

  
//Fill my model from the collection and validates it.
  
TryUpdateModel(profile, values);

  
if(ModelState.
IsValid)
   {
      profile
.
Save();
     
return
RedirectToAction("Index", "Home");
   }
  
else
//Show validation errors
      return
View();
}
 

 

 

Explanation

The built in server side validation for MVC 2 will only work if your action accepts the a Model object as a parameter. This causes MVC to create the model object and automatically map the incoming form input values to it.  As part of this process, it will also check to see whether the DataAnnotation validation attributes for the model are valid.  If everything is valid, then the ModelState.IsValid check within our code will return true.

When your action accepts a FormCollection this does not happen therefore, the DataAnnotation validation attributes are never evaluated and the ModelState.IsValid is never set.  We can correct this issue by simply performing  those steps ourselves.  MVC 2 provides functions that we can use to do this (UpdateModel and TryUpdateModel).  The difference is Update will throw if there is a validation error whereas TryUpdate will simply set the IsValid to false.

An added benefit of using the UpdateModel is that an empty model will be populated with all the values from the form collection for you.

 

Tags: ,

Work

I can't spell

by Donovan Brown 15. December 2010 17:53

Problem:

I can't spell.

Solution:

Download the Spell Checker Extension for Visual Studio 2010!

http://visualstudiogallery.msdn.microsoft.com/en-us/7c8341f1-ebac-40c8-92c2-476db8d523ce

 

Tags:

Work

About the author

My name is Donovan Brown and I am a process consultant for Imaginet with a background in application development.  I also run one of the Nation’s fastest growing online registration sites for motorsports events DLBRacing.com.  When I am not writing software I race cars for fun.  DLBRacing.com has given me the opportunity to combine my two passions writing software and racing cars.

AdSense

Month List

AdSense