Metadata and Validation

Hey guys! Long time no see eh? Anyway, I have what will hopefully be a quick question. It probably stems from a change in the mvc rc2 that I just installed, over what I had.

I have the following code. What’s happening is, on postback, the required attribute for sequence will fire. If it passed, the range check is skipped completely. If it fails, it never gets to the range check either. Have I coded something wrong?

using System.ComponentModel.DataAnnotations;
namespace Venue.Models
{
    [MetadataType(typeof(CategoryMetadata))]
    public partial class Category
    {
    }
    public class CategoryMetadata
    {
        [Required(ErrorMessageResourceType = typeof(Resources.Locale), ErrorMessageResourceName = "sequence_required")]
        [Range(0, 9999, ErrorMessageResourceType = typeof(Resources.Locale), ErrorMessageResourceName = "sequence_invalid")]
        public int Sequence { get; set; }
        [Required(ErrorMessageResourceType = typeof(Resources.Locale), ErrorMessageResourceName = "title_required")]
        [StringLength(256, ErrorMessageResourceType = typeof(Resources.Locale), ErrorMessageResourceName = "title_invalid")]
        public string Title { get; set; }
    }
}

Would this be the cause of the problem.

note: code below is from the System.ComponentModel.DataAnnotations.RangeAttribute class


public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
if ((value is string) && string.IsNullOrEmpty((string) value))
{
return true;
}
object obj2 = this.Conversion(value);
IComparable minimum = (IComparable) this.Minimum;
IComparable maximum = (IComparable) this.Maximum;
return ((minimum.CompareTo(obj2) <= 0) && (maximum.CompareTo(obj2) >= 0));
} 
 

so if the “value” is a null or empty string the range validator returns true for IsValid

Heya Pufa. That’s very interesting to know, but that wasn’t the problem. It was in the action itself. I never checked ModelState.IsValid, so it just proceeded to update the values and move on, instead of redisplaying the form.

=/

Just curious, and semi-off topic, Serena are you using MVC 1.0, or 2.0? And, if it is 2.0, is it a giant leap from 1.0 (ie, enterprise ready), and your thought of it?

It’s 2.0 RC2, though I started with version 1.0 rtm last year.

The judgement as to whether or not it is enterprise ready truly depends on the needs and coding practices on your enterprise, though I would think that upon RTM release, version 2.0 is suitable, with the aide of a few external libraries, such as FluentNHibernate and Castle’s IoC stuff, enterprise would benefit from it.

Personally though, I would like to see a IoC and DI system eventually built in to MVC. I really don’t like having to constantly upgrade third party libraries, just to adjust my own code to suit changes they made. I recently experienced this when moving from StructureMap 2.5 to 2.6 and consequently went back to poor man’s DI for now (let’s face it, it’s dang simple to do and still allows for testing).

Though a lot has changed internally, it is not a big deal to migrate.

On the whole though, I’d say yes, it’s enterprise ready. Not perfect, but ready.

Thanks for the reply Serena, it did anwser a few questions. For my non-profit app, I think I will go the MVC route, even if it hasn’t matured enough. I mean, 2.0 is still an infant in technology terms. Now, I need a good MVC tutorial/reference book, but I might have to wait a acouple months after .NET 4.0 and VS2010 is released End of this month - Beginning of next.

BTW, glad to have you back on the forums and working on you app again. :slight_smile:

I’ve been using Mvc since Preview 1 of Mvc 1 in enterprise application without a problem. Well, it was far more painful using the earlier previews before any RTM, but that’s really settled and the preview and beta releases of Mvc2 have been better to work with