开发者

Html.ValidationMessageFor on Mono has strange results

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)]
开发者_StackOverflow中文版   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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜