gpt4 book ai didi

asp.net-mvc-3 - Asp.net MVC maxlength 不使用数据注释工作

转载 作者:行者123 更新时间:2023-12-04 03:34:14 26 4
gpt4 key购买 nike

我正在使用 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; }
}
}

最佳答案

回答自己以帮助 future 的读者。我发现 Maxlength 和 Minlength 用于字符串而不是整数。

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

关于asp.net-mvc-3 - Asp.net MVC maxlength 不使用数据注释工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13630389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com