Data Annotaions(Model 验证)
Model验证
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication3.Models
{
public class Movie
{
public int Id { get; set; }
[Required]
[StringLength(100)]
public string Title { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
[Required]
[StringLength(1000)]
public string Description { get; set; }
[Range(0,999.99)]
public decimal Price { get; set; }
}
}
- [CreditCard]
- [Compare]
- [EmailAddress]
- [Phone]
- [Range]
- [RegularExpression]
- [Required]
- [StringLength]
- [Url]
- [Display]
- [DataType(DateType=)]
防止跨站请求伪造验证
使用[ValidateAntiForgeryToken]
public class HomeController : Controller
{
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Index(int id)
{
StudentsViewModel svm = new StudentsViewModel();
var s= svm.GetStudentById(id);
if(s==null)
}
}
界面显示验证问题
显示单个错误信息
<span asp-validation-for="@Model.FirstName"></span>
验证信息提示汇总
<div asp-validation-summary="All"></div>
Filed under: ASP.NET Core,C#,编程 - @ 2022年4月9日 下午5:15