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


相关推荐
带刺的坐椅4 小时前
无耳科技 Solon v3.0.7 发布(2025农历新年版)
java·spring·mvc·solon·aop
鱼骨不是鱼翅10 小时前
Spring Web MVC基础第一篇
前端·spring·mvc
hong_zc12 小时前
Spring MVC (三) —— 实战演练
java·spring·mvc
weixin_422778892 天前
spring mvc 创建restapi 笔记
笔记·spring·mvc
我要学编程(ಥ_ಥ)4 天前
初始JavaEE篇 —— Spring Web MVC入门(上)
spring boot·spring·java-ee·mvc
码农小灰4 天前
Spring MVC中HandlerInterceptor的作用及应用场景
java·spring boot·后端·spring·mvc
jyl_sh6 天前
使用Python和Qt6创建GUI应用程序--前言
python·mvc·客户端·pyside6
lixww.cn6 天前
ASP.NET Core MVC
c#·mvc·.netcore
牛马程序员‍6 天前
Day99 Gitub、系统分层架构
git·架构·mvc·ddd架构·gitub
码农小灰7 天前
Spring MVC 中的 DispatcherServlet:工作流程与应用场景解析
java·spring·mvc