ASP.NET MVC 模型验证不适用于控制器构造函数之一
Posted
技术标签:
【中文标题】ASP.NET MVC 模型验证不适用于控制器构造函数之一【英文标题】:ASP.NET MVC Model validation not working on one of the controller constructor 【发布时间】:2021-12-29 17:34:37 【问题描述】:我有用户注册、登录和更新的模型验证。但是,模型验证仅适用于登录和注册。更新功能也没有响应 型号
namespace DDemo.Models
public class SysUser
[Required(ErrorMessage = "Please enter User ID")]
[Remote(action: "VerifyUserID", controller: "Account")]
public string InvestorId get; set;
[Required(ErrorMessage = "Please enter Password")]
[StringLength(20, MinimumLength = 5, ErrorMessage = "Password must be 5 characters or more")]
public string InvestorPw get; set;
[Compare("InvestorPw", ErrorMessage = "Passwords do not match")]
public string InvestorPw2 get; set;
[Required(ErrorMessage = "Please enter Full Name")]
public string FullName get; set;
[Required(ErrorMessage = "Please enter Email")]
[EmailAddress(ErrorMessage = "Invalid Email")]
public string Email get; set; //@ sign
[Required(ErrorMessage = "Please enter Contact Number")]
[RegularExpression(@"\d8", ErrorMessage = "Please enter 8 digit number")]
public string Contact get; set;
public DateTime LastLogin get; set;
控制器
[Authorize]
[HttpPost]
public IActionResult UserUpdate(SysUser user)
if (!ModelState.IsValid)
ViewData["Message"] = "Invalid Input";
ViewData["MsgType"] = "warning";
return View("UserUpdate", user);
else
string update =
@"
UPDATE Investor
SET InvestorPw = ('SHA1', '1'), FullName = '(2)', Email = '3', Contact = '4'
WHERE InvestorId = '0'";
int result = DBUtl.ExecSQL(update, user.InvestorId, user.InvestorPw, user.FullName, user.Email, user.Contact);
if (result == 1)
TempData["Message"] = "Account Updated";
TempData["MsgType"] = "success";
else
TempData["Message"] = DBUtl.DB_Message;
TempData["MsgType"] = "danger";
return RedirectToAction("Users");
景色
@model SysUser
<form asp-controller="Account"
asp-action="UserUpdate"
method="post">
<div class="form-group row">
<div class="offset-sm-0 col-sm-4">
<h2>Update Your Account</h2>
</div>
</div>
</form>
<div class="form-group row">
<label class="control-label col-sm-2" asp-for="InvestorId">User ID :</label>
<div class="col-sm-4">
<input type="text" asp-for="InvestorId" readonly="readonly" class="form-control" />
</div>
<div class="col-sm-3">
<span asp-validation-for="InvestorId" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" asp-for="FullName">Full Name :</label>
<div class="col-sm-4">
<input type="text" asp-for="FullName" class="form-control" />
</div>
<div class="col-sm-3">
<span asp-validation-for="FullName" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" asp-for="Email">Email ID :</label>
<div class="col-sm-4">
<input type="text" asp-for="Email" class="form-control" />
</div>
<div class="col-sm-3">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" asp-for="InvestorPw">Password :</label>
<div class="col-sm-4">
<input type="password" asp-for="InvestorPw" class="form-control" />
</div>
<div class="col-sm-3">
<span asp-validation-for="InvestorPw" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" asp-for="InvestorPw2">Confirm :</label>
<div class="col-sm-4">
<input type="password" asp-for="InvestorPw2" class="form-control" />
</div>
<div class="col-sm-3">
<span asp-validation-for="InvestorPw2" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" asp-for="Contact">Contact :</label>
<div class="col-sm-4">
<input type="text" asp-for="Contact" class="form-control" />
</div>
<div class="col-sm-3">
<span asp-validation-for="Contact" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="offset-sm-2 col-sm-6">
<input type="submit" value="Submit" class="btn btn-primary" />
</div>
</div>
@if (ViewData["Message"] != null)
<div class="form-group row">
<div class="alert alert-@ViewData["MsgType"]">
<text>@ViewData["Message"]</text>
</div>
</div>
enter image description here
enter image description here 如图所示,模型验证适用于注册但不适用于更新
【问题讨论】:
【参考方案1】:您必须修复视图的表单标签,将表单末尾移动到提交按钮下方并添加@html.ValidationSummary()
<form asp-controller="Account"
asp-action="UserUpdate"
method="post">
@Html.ValidationSummary(true)
<div class="form-group row">
<div class="offset-sm-0 col-sm-4">
<h2>Update Your Account</h2>
</div>
</div>
........
<div class="form-group row">
<div class="offset-sm-2 col-sm-6">
<input type="submit" value="Submit" class="btn btn-primary" />
</div>
</div>
</form>
【讨论】:
以上是关于ASP.NET MVC 模型验证不适用于控制器构造函数之一的主要内容,如果未能解决你的问题,请参考以下文章
有效的 RegEx 不适用于 ASP.Net MVC 正则表达式属性
RedirectToAction 不适用于 Tempdata - ASP.net 核心 MVC
Asp.Net MVC 5 Jquery 验证不适用于带有提交事件的 ajax 帖子,显示为有效表单
ASP.Net MVC 4 Web API 控制器不适用于 Unity.WebApi