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

Categories

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),然后才显示绑定失败警告(如“用户名不能为空!”),为什么不是设计在客户端验证成功才调用服务端?这是微软设计的问题还是我理解水平不够的问题@_@


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
4.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...