在ASP.NET MVC中更改密码功能(无法从数据库中获取值) [英] Change password funtionality in ASP.NET MVC(unable to fetch value from database)

查看:83
本文介绍了在ASP.NET MVC中更改密码功能(无法从数据库中获取值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个MVC应用程序。当我从数据库中获取数据时,我在变量中得到一个空值。



我的型号类代码



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

My Model Class Code

public partial class tblUser
{
    public int Id { get; set; }

    [Required(ErrorMessage = "Please Enter Your Email Id")]
    [EmailAddress(ErrorMessage = "Invalid Email Address")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Please Enter Your Password")]
    [DataType(DataType.Password)]
    [StringLength(18, ErrorMessage = "The password must be atleast 3 characters long", MinimumLength = 3)]
    public string Password { get; set; }

    [Required(ErrorMessage = "Please Enter Your Password")]
    [DataType(DataType.Password)]
    [StringLength(18, ErrorMessage = "The password must be atleast 3 characters long", MinimumLength = 3)]
    public string NewPassword { get; set; }
}







这是我的更改密码查看代码






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>

       }





这是我的家庭控制器代码。



我面临的问题 - var userDetail返回null,当我使用断点检查并调试我的代码时我的login.Email没有从数据库中获取电子邮件,它返回null。





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);
       }





我的尝试:



我认为我的视图没有模型的Email属性的HiddenFor,因此在我的HttpPost方法中它将为null但它没有用完。



What I have tried:

I thought my view does not have a HiddenFor for the Email property of the model, so it will be null in my HttpPost method but it didn't work out.

推荐答案

您的问题出在以下几行:



Your problem lies on the following line:

var userDetail = db.tblUsers.FirstOrDefault(c => c.Email == login.Email);





i怀疑你希望它是:





i suspect you want it to be :

var userDetail = db.tblUsers.FirstOrDefault(c => c.Email == detail.Email);





因为登录isntance将使电子邮件为空,因为您没有通过从视图作为隐藏字段,所以它将为null,您现在正在基于密码匹配获取用户,如果找到任何行,您想要传递从该实例返回的emial用于获取用户详细信息ils。



请注意,基于密码获取行的方式不正确,因为多个用户可以使用相同的密码,因此如果用户1正在更新他的密码而user2也有相同的密码,那么用户将获得更新的密码会出现问题?想想那种情况。



because login isntance will have Email null as you are not passing that from view as hidden field so it will be null, you are fetching user on the basis of password match right now, and if any row found you are want to pass the emial returned from that instance to use to fetch user details.

Please note that, your way of getting row on the basis of password is not right, as multiple users can have same password con-incidently, so in that case there will be problems, if user 1 is updating his password and user2 also has same password, the password for which user will get updated? Think about that scenario.


这篇关于在ASP.NET MVC中更改密码功能(无法从数据库中获取值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆