In my previous post about model-based validation I introduced my concept of validating data using attributes as well as using my own HTML helpers to display errors. Many things have changed since then because of new MVC release. What’s new in ASP.NET MVC Preview 5?
You can read more about it in Scott gu’s article ASP.NET MVC Preview 5 and Form Posting Scenarios.
That’s why I have updated my code from last time to use some new stuff and to get much shorter code in controllers:
[ActionName("Create"), AcceptVerbs("POST")] public ActionResult Save() { if (this.IsModelValid(typeof(TestModel))) { // Save it, just an example _repository.Save<TestModel>(ViewData.Model); return RedirectToAction("Done"); } return View("Index"); }
What IsModelValid() does?
I really like this idea (code is very readable) but after reading Thoughts on validation in ASP.NET MVC applications I realized that my approach lacks in some ways:
So althrought Steve Sanderson’s code is a bit longer, his validation concept make sense and I am really looking forward to see it in action!
MVC is definitely the way to go for the future but ASP.NET MVC Preview 4 (the latest release) still lacks in lot of different areas. One of the most essential missing features is some kind of validation and I am not the only one who needs this stuff right now. That’s why I have written a few lines for validating my forms. It’s basically a concept which is not ready for production but I want to share it with you a get some feedback.
Quantasoft, a Slovakia hosting company, has a limited offer on their "Mini" hosting plan: a whole one year for free (saving you $48)!
Hosting parameters:
This limited offer is for first 100 orders and is valid to 3th August 2008. Quantasoft also provides a free ASP.NET hosting. Enjoy!
(Disclaimer: I am not connected to Quantasoft in any way)
Well… yes and no because none of them has been released yet. The only one available is "ASP.NET MVC in Action" from Mannings which is in early access program. Personally I am really curious about "Professional ASP.NET 3.5 MVC" book because Conery & Hanselman & Haack is a very interesting combination of great and famous developers. And what about you?
Imagine that we have some class (e.g. UserProfile) with Culture property for storing a culture specific string, in other words a culture name.
class UserProfile { private string _culture; public string Culture { get { return _culture; } set { _culture = value; } } }
Our goal is to allow only existing culture name to be set and avoid an invalid one. Let’s start by writing unit tests where we will try to assign valid and invalid values to our property (using MSTest):