Mono上的Html.ValidationMessageFor有奇怪的结果

时间:2010-08-19 20:40:15

标签: c# asp.net-mvc validation mono

我是ASP.NET的初学者,我正在学习如何通过this tutorial使用它。我使用Linux,所以我使用的是Mono 2.6.7。我不得不多次偏离教程的路径以便在Mono下工作(包括使用我将链接到的MVC 2的修补版本,但我无法为新用户发布一个超链接) ,但有一个我无法解决的问题:表单验证。我设置了这样的验证元数据:

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; }
  }
 }
}

我设置了这样的视图:

<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>

但是,当我通过提供不良数据来测试表单时,我没有收到我设置的错误消息。当我将“标题”留空时,我得到:“验证错误(System.ComponentModel.DataAnnotations.RequiredAttribute):标题”,当我将Price留空时,我收到类似的错误。如果我输入“Price”的超出范围的数字或“Title”或“AlbumArtUrl”的长字符串,脚本会忽略该问题,不会留下错误消息并让表单提交;但是,如果我在“价格”中输入一个非常大的数字,我会得到“价值'大数'无效。”。

我该如何解决这个问题?我可以使用Html.ValidationMessageFor的替代方法吗?我的模特有问题吗?这只是Mono的一个问题吗?请帮忙!

1 个答案:

答案 0 :(得分:0)

看起来[Required]和[Range]属性在Mono中并没有真正实现。它们只是存根,因此您可以编译应用程序。