Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

ASP .NET Core Razor的模型验证绑定是不是有点浪费服务器?

ASP .NET Core Razor的模型验证绑定每次都需要回发给服务端进行检查?是不是有点浪费服务器。
我用的Demo如下:

<form method="POST">
<div>
<input asp-for="UserName" type="text" placeholder="用户名" />
<span asp-validation-for="UserName"></span>
</div>
<div>
<input asp-for="Password" type="password" placeholder="密码" />
<span asp-validation-for="Password"></span>
</div>
<button type="submit">登录</button>
</form>

==============================
Model端:

[BindProperty]
[Required(ErrorMessage = "用户名不能为空!")]
public string UserName { get; set; } = "";

[BindProperty]
[Required(ErrorMessage = "密码不能为空!")]
public string Password { get; set; } = "";

public IActionResult OnPostAsync()
{
if (ModelState.IsValid == true)
{
//搞点事情
}
return Page();
}

如果前端里用户名或者密码任意一项没填,都要发给服务端验证(调用OnPostAsync),然后才显示绑定失败警告(如“用户名不能为空!”),为什么不是设计在客户端验证成功才调用服务端?这是微软设计的问题还是我理解水平不够的问题@_@


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...