I have model validation for user register,login and update. However the model validation only works on login and register. The update function does not respond either The model
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; }
}
}
The controller
[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");
}
}
The view
@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 As in the picture, model validation works with register but not with update
CodePudding user response:
you have to fix a form tag of your view, move the end of form to below of a submit button and add @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>