Model validation in ASP.NET Core MVC and Razor Pages
Model validation in ASP.NET Core MVC and Razor Pages
[Remote] attribute
The [Remote]
attribute implements client-side validation that requires calling a method on the server to determine whether field input is valid. For example, the app may need to verify whether a user name is already in use.
To implement remote validation:
-
Create an action method for JavaScript to call. The jQuery Validate remote method expects a JSON response:
true
means the input data is valid.false
,undefined
, ornull
means the input is invalid. Display the default error message.- Any other string means the input is invalid. Display the string as a custom error message.
Here's an example of an action method that returns a custom error message:
C#
-
[AcceptVerbs("GET", "POST")] public IActionResult VerifyEmail(string email) { if (!_userService.VerifyEmail(email)) { return Json($"Email {email} is already in use."); } return Json(true); }
-
In the model class, annotate the property with a
[Remote]
attribute that points to the validation action method, as shown in the following example:C#
[Remote(action: "VerifyEmail", controller: "Users")]
public string Email { get; set; }
The [Remote]
attribute is in the Microsoft.AspNetCore.Mvc
namespace.
Additional fields
The AdditionalFields
property of the [Remote]
attribute lets you validate combinations of fields against data on the server. For example, if the User
model had FirstName
and LastName
properties, you might want to verify that no existing users already have that pair of names. The following example shows how to use AdditionalFields
:
[Remote(action: "VerifyName", controller: "Users", AdditionalFields = nameof(LastName))]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Remote(action: "VerifyName", controller: "Users", AdditionalFields = nameof(FirstName))]
[Display(Name = "Last Name")]
public string LastName { get; set; }
AdditionalFields
could be set explicitly to the strings "FirstName" and "LastName", but using the nameof operator simplifies later refactoring. The action method for this validation must accept both firstName
and lastName
arguments:
[AcceptVerbs("GET", "POST")]
public IActionResult VerifyName(string firstName, string lastName)
{
if (!_userService.VerifyName(firstName, lastName))
{
return Json($"A user named {firstName} {lastName} already exists.");
}
return Json(true);
}
When the user enters a first or last name, JavaScript makes a remote call to see if that pair of names has been taken.
To validate two or more additional fields, provide them as a comma-delimited list. For example, to add a MiddleName
property to the model, set the [Remote]
attribute as shown in the following example:
[Remote(action: "VerifyName", controller: "Users", AdditionalFields = nameof(FirstName) + "," + nameof(LastName))]
public string MiddleName { get; set; }
AdditionalFields
, like all attribute arguments, must be a constant expression. Therefore, don't use an interpolated string or call Join to initialize AdditionalFields
.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2016-03-31 DockPanel的使用