MVC之 Controller 》》 ModelState ValidationMessageFor ValidationSummary

在这里插入图片描述
ModelState是Controller的一个属性,可以被继承自System.Web.Mvc.Controller的那些类访问。它表示在一次POST提交中被提交到服务器的 键值对集合,每个记录到ModelState内的值都有一个错误信息集。尽管ModelState的名字中含有“Model”,但它只有名称、值和错误集,与任何Model类都没有关系。
》》 ModelState有两个作用:
1:存储提交到服务器的值,
2:存储与之相关联的验证错误集。

在这里插入图片描述
在这里插入图片描述

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

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控件将会读取模型中所有属性的错误信息摘要并显示在一个项目符号列表中;

@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 自定义错误

[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");
}

在这里插入图片描述
在这里插入图片描述

相关推荐

  1. Spring MVC的<mvc:view-controller>标签

    2024-07-11 14:52:01       30 阅读
  2. Spring MVCcontroller方法返回值

    2024-07-11 14:52:01       53 阅读
  3. MVC(Model-View-Controller)架构简介

    2024-07-11 14:52:01       20 阅读
  4. Spring MVCController切面拦截不起作用?

    2024-07-11 14:52:01       52 阅读
  5. Spring MVC中@Controller和@RestController的区别

    2024-07-11 14:52:01       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 14:52:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 14:52:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 14:52:01       58 阅读
  4. Python语言-面向对象

    2024-07-11 14:52:01       69 阅读

热门阅读

  1. C语言-概述,应用领域

    2024-07-11 14:52:01       19 阅读
  2. c++ 网络编程udp协议 poco模块

    2024-07-11 14:52:01       21 阅读
  3. 动态规划算法-以中学排课管理系统为例

    2024-07-11 14:52:01       25 阅读
  4. ThingsBoard本地windows环境启动

    2024-07-11 14:52:01       26 阅读
  5. Oracle各种连接写法介绍

    2024-07-11 14:52:01       24 阅读
  6. C# 委托和事件

    2024-07-11 14:52:01       20 阅读