I am using ModelState.AddModelError in MVC to show error messages in view. I have this code:
[HttpPost]
[Route("resetpassword")]
public ActionResult ResetPassword(ResetPasswordView resetPasswordView)
{
if(resetPasswordView.Password == resetPasswordView.ConfirmPassword)
{
.....
}
else
{
ModelState.AddModelError("", "Should be same password");
return View("ResetPassword");
}
return View("ThankYouResetPassword");
}
and this is the view that shows 3 textboxe that user enter email and password and when click on submit the method above will execute, I need to show a message when 2 textboxed are not same.
@model Models.ResetPasswordView
<div class="page resetPassword">
@using (Html.BeginForm("resetpassword", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
<h5>Reset Your Loan Center Password</h5>
<table>
<tr><td>Email Address</td><td><input type="email" name="Email" placeholder="jdoe@example.com" readonly value="@Model.Email" size="25"></td></tr>
<tr><td>Password</td><td><input type="Password" name="Password" placeholder="Create Password" size="25"></td></tr>
<tr><td>Confirm Password</td><td><input type="Password" name="ConfirmPassword" placeholder="Re-enter Password" size="25"></td></tr>
<tr><td colspan="2"><input type="submit" value="Reset Password"></td></tr>
<tr>
<td class="errMessage" colspan="2">
@Html.ValidationSummary(true)
</td>
</tr>
</table>
}
So when code goes to else part that needs to show the error message and goes to view I am getting the null exception in this line:
@using (Html.BeginForm("resetpassword", "Home", FormMethod.Post))
This is error I am getting:
System.NullReferenceException: Object reference not set to an instance of an object.