MVC之 Controller 》》 ModelState ValidationMessageFor ValidationSummary

ModelState是Controller的一个属性,可以被继承自System.Web.Mvc.Controller的那些类访问。它表示在一次POST 提交中被提交到服务器的 键值对集合,每个记录到ModelState内的值都有一个错误信息集。尽管ModelState的名字中含有"Model",但它只有名称、值和错误集,与任何Model类都没有关系。

》》 ModelState有两个作用:

1:存储提交到服务器的值,

2:存储与之相关联的验证错误集。


ModelState 中错误集合,是记录 Model中特性标识,Require、StringLength、EmailAddress等

csharp 复制代码
public class XXXModel
{
    [Required(ErrorMessage = "Please enter the user's first name.")]
    [StringLength(50, ErrorMessage = "The First Name must be less than {1} characters.")]
    [Display(Name = "First Name:")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Please enter the user's last name.")]
    [StringLength(50, ErrorMessage = "The Last Name must be less than {1} characters.")]
    [Display(Name = "Last Name:")]
    public string LastName { get; set; }

    [EmailAddress(ErrorMessage = "The Email Address is not valid")]
    [Required(ErrorMessage = "Please enter an email address.")]
    [Display(Name = "Email Address:")]
    public string EmailAddress { get; set; }
}

同时要在csHTML 中 @Html.ValidationMessageFor()

》》请注意我们现在使用的两个帮助信息控件 ValidationSummary 和 ValidationMessageFor。

》》ValidationMessageFor则只显示其指定属性的错误信息摘要。

》》ValidationSummary控件将会读取模型中所有属性的错误信息摘要并显示在一个项目符号列表中;

html 复制代码
@model ModelStateDemo.ViewModels.Home.xxxModel

<h2>Add</h2>

@using(Html.BeginForm())
{
    @Html.ValidationSummary()
    <div>
        <div>
            @Html.LabelFor(x => x.FirstName)
            @Html.TextBoxFor(x => x.FirstName)
            @Html.ValidationMessageFor(x => x.FirstName)
        </div>
        <div>
            @Html.LabelFor(x => x.LastName)
            @Html.TextBoxFor(x => x.LastName)
            @Html.ValidationMessageFor(x => x.LastName)
        </div>
        <div>
            @Html.LabelFor(x => x.EmailAddress)
            @Html.TextBoxFor(x => x.EmailAddress)
            @Html.ValidationMessageFor(x => x.EmailAddress)
        </div>
        <div>
            <input type="submit" value="Save" />
        </div>
    </div>
}

ModelState 自定义错误

csharp 复制代码
[HttpPost]
public ActionResult Add(AddUserVM model)
{
    if(model.FirstName == model.LastName)
    {
        ModelState.AddModelError("LastName", "The last name cannot be the same as the first name.");
    }
    if(!ModelState.IsValid)
    {
        return View(model);
    }
    return RedirectToAction("Index");
}


相关推荐
bing_15815 小时前
一个 HTTP 请求进入 Spring MVC 应用后,大致经历了哪些主要步骤?
spring·http·mvc
电商api接口开发2 天前
ASP.NET MVC 实现增删改查(CRUD)操作的完整示例
后端·asp.net·mvc
CF14年老兵2 天前
MVC 应用程序中使用 FluentValidation 进行验证的重要性
性能优化·mvc·.net
葵续浅笑2 天前
Spring之我见 - Spring MVC重要组件和基本流程
java·spring·mvc
王有品2 天前
Spring MVC 一个简单的多文件上传
java·spring·mvc
王有品3 天前
Spring MVC 核心注解与文件上传教程
java·spring·mvc
bing_1583 天前
Spring MVC DispatcherServlet 的作用是什么? 它在整个请求处理流程中扮演了什么角色?为什么它是核心?
java·spring·mvc
匆匆整棹还3 天前
关于springmvc的404问题的一种猜测解决方案
spring·mvc
全栈小53 天前
【C#】.net core 6.0调用MVC API接口时,提示Unsupported Media Type,状态码415
c#·mvc·.netcore
zzx_nihao3 天前
Java29:Spring MVC
java·spring·mvc