1

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.
Alma
  • 3,780
  • 11
  • 42
  • 78
  • There are a number of issues with your ResetPassword controller method. Please review MSDN documentation on how best to build controller methods. e.g. https://www.asp.net/mvc/overview/getting-started/introduction/examining-the-edit-methods-and-edit-view – Aaron Hudon Nov 07 '16 at 22:11
  • 1
    Next time, please use the [tag:asp.net-mvc] tag. The [tag:model-view-controller] tag is for question about the pattern in general. – Heretic Monkey Nov 07 '16 at 22:23
  • 1
    Go to the MVC site and work through the tutorials to learn the basics of creating views in MVC - use the strongly typed `HtmlHelper` methods to generate your form controls, apply the `[Compare]` attribute to your `ConfirmPassword` property and include `@Html.ValidationMessageFor()` in your view. All this is handled out out the box by MVC. –  Nov 07 '16 at 22:37
  • And as a side note - your error is thrown my ` value="@Model.Email"` because you did not return the model in the POST method so its `null` (but of course none of this would happen if you had generated you view correctly in the first place. –  Nov 07 '16 at 22:39

0 Answers0