Asp.net MVC maxlength无法使用数据注释

Gar*_*rry 2 asp.net-mvc-3

我正在使用asp.net mvc 4我在我的模型中使用[maxlength(2)]但是它不能用于客户端验证我是asp.net mvc的新手.这是我的代码.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace RestrauntsMVC.Models
{
  public class Restraunts
  {

    public int id { get; set; }
    [Required]
    public string name { get; set; }
    [Required]
    [MaxLength(2),MinLength(1)]
    public int rating { get; set; }
    [Required]
    public string location { get; set; }
  }
}
Run Code Online (Sandbox Code Playgroud)

Gar*_*rry 6

我发现答案myslef它工作Maxlength和minlength是String不是整数.

using System;
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.ComponentModel.DataAnnotations;  
namespace RestrauntsMVC.Models
{
public class Restraunts
{

    public int id { get; set; }
    [Required]
    public string name { get; set; }
    [Required]
    [Range(1,10,ErrorMessage="Rating must between 1 to 10")]        
    public int rating { get; set; }
    [Required]
    public string location { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)

  • MaxLength用于指定属性的最大长度,而不是最大数值:http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.maxlengthattribute%28v=vs.103%29. ASPX (2认同)
  • @WiiMaxx因为我找到自己的答案,我想帮助其他将来也会阅读的用户. (2认同)