0

I am creating an MVC application. I am getting a null value in variable when I fetching data from database.

Here is my change password view code

@using (Html.BeginForm("Changepassword", "Home", FormMethod.Post))
{

       <table class="center">


           <tr>
               <td>Old Password</td>
               <td>
                   @Html.EditorFor(pass => pass.Password)
               </td>
               <td>@Html.ValidationMessageFor(pass => pass.Password)</td>
           </tr>
           <tr class="rowspace">
               <td>New Password</td>

               <td>
                   @Html.EditorFor(pass => pass.NewPassword)
               </td>
               <td>@Html.ValidationMessageFor(pass => pass.NewPassword)</td>
           </tr>

           <tr class="rowspace">
               <td colspan="3" id="button">
                   <input type="submit" value="Change Password" /></td>
           </tr>
           <tr class="rowspace"><td colspan="3">@ViewBag.Message</td></tr>
       </table>

}

Here is my Home controller code.

Problem I am facing - var userDetail is returning null and when i checked and debug my code using breakpoint my login.Email is not fetching email from database and it is returning null.

public ActionResult Changepassword(tblUser login)
{
   using (UserDetailsEntities db = new UserDetailsEntities())
   {
       var detail = db.tblUsers.Where(log => log.Password == login.Password).FirstOrDefault();
       if (detail != null)
       {
           var userDetail = db.tblUsers.FirstOrDefault(c => c.Email == login.Email);


           if (userDetail != null)
           {
               userDetail.Password = login.NewPassword;

               db.SaveChanges();
               ViewBag.Message = "Record Inserted Successfully!";
           }
           else
           {
               ViewBag.Message = "Password not Updated!";
           }

       }
   }

   return View(login);
}
6
  • 2
    in your view for the model email is not assigned.so it will come as null in tblUser login make a hidden field for the email.. Commented May 1, 2016 at 5:57
  • And are you really storing your passwords in the database as plain text (not hashed)? Commented May 1, 2016 at 5:58
  • @Sachu can you explain with code .... as I am very new to MVC and coding.It will be very helpful Commented May 1, 2016 at 6:02
  • @AmanM two ways of doing it..1. Like you are asking old password ask for the email in the view. Or pass the model from controller and save the email value in a hidden field.I hope first one is the best practice. Commented May 1, 2016 at 6:03
  • Just add @Html.EditorFor(m => m.Email) in your view and change the controller to var detail = db.tblUsers.Where(x => x.Password == login.Password && x.Email == login.Email).FirstOrDefault(); - the second query is not required Commented May 1, 2016 at 6:04

1 Answer 1

1

In your view email for the model is not assigning so it will be always null when its get posted. Best practice is to ask for the email also at the time of password change. Check code below Your view

 @using (Html.BeginForm("Changepassword", "Home", FormMethod.Post))
    {
           <table class="center">
             <tr>
                   <td>Email</td>
                   <td>
                       @Html.EditorFor(pass => pass.email)
                   </td>
                   <td>@Html.ValidationMessageFor(pass => pass.email)</td>
               </tr>
               <tr>
                   <td>Old Password</td>
                   <td>
                       @Html.EditorFor(pass => pass.Password)
                   </td>
                   <td>@Html.ValidationMessageFor(pass => pass.Password)</td>
               </tr>
               <tr class="rowspace">
                   <td>New Password</td>

                   <td>
                       @Html.EditorFor(pass => pass.NewPassword)
                   </td>
                   <td>@Html.ValidationMessageFor(pass => pass.NewPassword)</td>
               </tr>

               <tr class="rowspace">
                   <td colspan="3" id="button">
                       <input type="submit" value="Change Password" /></td>
               </tr>
               <tr class="rowspace"><td colspan="3">@ViewBag.Message</td></tr>
           </table>

    }

Controller

 public ActionResult Changepassword(tblUser login)
    {
       using (UserDetailsEntities db = new UserDetailsEntities())
       {
           var detail = db.tblUsers.Where(log => log.Password == login.Password 
           && log.email == login.email).FirstOrDefault();
           if (detail != null)
           {
                   userDetail.Password = login.NewPassword;

                   db.SaveChanges();
                   ViewBag.Message = "Record Inserted Successfully!";

              }
     else
               {
                   ViewBag.Message = "Password not Updated!";
               }


       }

       return View(login);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.