ASP.NET MVC 模型验证不适用于控制器构造函数之一
ASP.NET MVC Model validation not working on one of the controller constructor
我有用户注册、登录和更新的模型验证。但是,模型验证仅适用于登录和注册。更新功能也没有响应
型号
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(@"\d{8}", 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
如图所示,模型验证适用于 register 但不适用于 update
您必须修复视图的表单标签,将表单末尾移至提交按钮下方并添加@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>
我有用户注册、登录和更新的模型验证。但是,模型验证仅适用于登录和注册。更新功能也没有响应 型号
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(@"\d{8}", 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 如图所示,模型验证适用于 register 但不适用于 update
您必须修复视图的表单标签,将表单末尾移至提交按钮下方并添加@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>