views:

68

answers:

1

Hi, I'm a beginner at ASP.NET and I was learning how to use it through this tutorial. I use Linux, so I'm using Mono 2.6.7. I've had to stray off the path of the tutorial several times to get things to work under Mono (including using a patched version of MVC 2 that I would link to, but I am unable to as new users can only post one hyperlink), but there's one problem that I haven't been able to fix: form validation. I set up my validation metadata like this:

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace MvcMusicStore.Models
{
 [MetadataType(typeof(AlbumMetaData))]
 public partial class Album
 {
  [Bind(Exclude = "AlbumID")]
  public class AlbumMetaData
  {
   [ScaffoldColumn(false)]
   public object AlbumID { get; set; }

   [DisplayName("Genre")]
   public object GenreID { get; set; }

   [DisplayName("Artist")]
   public object ArtistID { get; set; }

   [Required(ErrorMessage = "An Album Title is required")]
   [StringLength(160)]
   public object Title { get; set; }

   [DisplayName("Album Art URL")]
   [StringLength(1024)]
   public object AlbumArtUrl { get; set; }

   [Required(ErrorMessage = "Price is required")]
   [Range(0.01, 100.00, ErrorMessage = "Price must be between 0.01 and 100.00")]
   public object Price { get; set; }
  }
 }
}

And I set up my View like this:

<p>
    <%= Html.LabelFor(model => model.Title) %>
 <%= Html.TextBoxFor(model => model.Title) %>
    <%= Html.ValidationMessageFor(model => model.Title) %>
</p>

<p>
 <%= Html.LabelFor(model => model.Price) %>
 <%= Html.TextBoxFor(model => model.Price) %>
 <%= Html.ValidationMessageFor(model => model.Price) %>
</p>

<p>
 <%= Html.LabelFor(model => model.AlbumArtUrl) %>
 <%= Html.TextBoxFor(model => model.AlbumArtUrl) %>
 <%= Html.ValidationMessageFor(model => model.AlbumArtUrl) %>
</p>

However, when I test the form by giving it bad data, I don't get the error messages I set. When I leave "Title" blank, I get: "Validation error (System.ComponentModel.DataAnnotations.RequiredAttribute): Title", and I get a similar error when I leave Price blank. If I enter an out-of-range number for "Price" or a long string for "Title" or "AlbumArtUrl", the script ignores the problem, doesn't leave an error message and lets the form submit; however, if I type a very large number into "Price", I get "The value 'large number' is invalid.".

How can I fix this? Is there an alternate method to Html.ValidationMessageFor that I can use? Is it a problem with my model? Is it just a problem with Mono? Please help!

A: 

It looks like the [Required] and [Range] attributes aren't really implemented in Mono. They are just stubs so that you can compile your application.

jpobst
And [StringLength] too? Ah, okay, thanks. Is there any replacement I could use that does work in Mono? Some type of custom validation or something?
hatkirby
Actually, wait, I don't believe that's necessarily correct. [Required] definitely validates correctly because it prevents a form from being submitted if a field is left blank--it just shows the wrong error message. I even implemented my own custom RequiredAttribute class, and it still gave the bad error message.
hatkirby
Okay, it turns out that you were pretty much right--Mono hasn't really completed those classes, but they should be done for the next public release, 2.8, which will hopefully come soon. Until then, I will have to use the alternative of writing custom validations, which does work, even though it's annoying to do.
hatkirby