日期和十进制验证ASP.NET Core

时间:2018-08-08 07:23:15

标签: c# asp.net-core asp.net-core-mvc

我有个人模型 这是课程

 public partial class Persons
{
    public int Id { get; set; }
    [Required(ErrorMessage = "Please write First Name")]
    public string FirstName { get; set; }
    [Required(ErrorMessage = "Please write Last Name")]
    public string LastName { get; set; }
    [StringLength(11, MinimumLength = 3, ErrorMessage = "Personal Number is too long")]
    [Remote(action: "PersonalNumberExists", controller: "Persons", ErrorMessage = "Personal Number is duplicated")]
    public string PersonalNumber { get; set; }
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? Birthdate { get; set; }
    public string Gender { get; set; }
    public decimal? Salary { get; set; }
}

这是视图

<div class="row">
<div class="col-md-4">
    <form asp-action="Create">
       <div class="form-group">
            <label asp-for="FirstName" class="control-label"></label>
            <input asp-for="FirstName" class="form-control" />
            <span asp-validation-for="FirstName" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="LastName" class="control-label"></label>
            <input asp-for="LastName" class="form-control" />
            <span asp-validation-for="LastName" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="PersonalNumber" class="control-label"></label>
            <input asp-for="PersonalNumber" class="form-control" />
            <span asp-validation-for="PersonalNumber" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Birthdate" class="control-label"></label>
            <input asp-for="Birthdate" class="form-control" />
            <span asp-validation-for="Birthdate" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Gender" class="control-label"></label>
            <input asp-for="Gender" class="form-control" />
            <span asp-validation-for="Gender" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Salary" class="control-label"></label>
            <input asp-for="Salary" class="form-control" />
            <span asp-validation-for="Salary" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </form>
</div>

我需要对出生日期和薪水字段进行日期和十进制验证。 我的意思是只检查日期是否为十进制(十进制验证且大于等于0) 我找不到任何验证 我该怎么做?

1 个答案:

答案 0 :(得分:0)

要进行验证,请尝试以下操作

[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
[Required]
public DateTime? Birthdate { get; set; }
public string Gender { get; set; }

[Required]
public decimal? Salary { get; set; }