用于MVC4中验证的数据注释或jQuery

时间:2013-06-25 05:19:22

标签: asp.net-mvc-4

您能否建议哪些是在数据注释验证器和jQuery验证之间处理MVC4中的数据验证的更好方法,或者我不知道的任何其他方式?

提前致谢...

2 个答案:

答案 0 :(得分:1)

首先来看看这个:

namespace MvcMusicStore.Models
{
public class Album
{
    [ScaffoldColumn(false)]
    public int      AlbumId    { get; set; }
    [DisplayName("Genre")]
    public int      GenreId    { get; set; }
    [DisplayName("Artist")]
    public int      ArtistId   { get; set; }

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

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

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

    public virtual Genre  Genre    { get; set; }
    public virtual Artist Artist   { get; set; }
}
}

使用数据注释很容易编写验证。但是,如果你想用jquery做到这一点,你必须自己手动编写所有代码。就这样!

并且,它支持所有类型的客户端验证:

  • 必填
  • StringLength
  • 范围等......

答案 1 :(得分:0)

向上面添加更多信息答案您还可以通过添加远程验证属性在服务器端编写自定义验证规则。

请求将在不加载网页的情况下与服务器异步。

相关问题