简体   繁体   中英

how to get data from Postgres data base with EntityFramework6.Npgsql in ASP.NET MVC?

I am creating a login application in ASP.NET MVC using Npgsql and EntityFramework6.Npgsql, I want to implement a function to retrieve the password through an identification number, that is, the user clicks the button "I forgot my password", then he must type the identification number and the program checks if it exists or is correct, after that, the user information to change the password is displayed.

The model class for user:

public partial class user
{

    public int id { get; set; }
    [Display(Name = "Id number")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "It's necessary the id number")]
    public string idnumber { get; set; }

    [Display(Name = "Name")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "It's necessary the name")]
    public string fname { get; set; }

    [Display(Name = "Last name")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "It's necessary the last name)]
    public string lname { get; set; }

    [Display(Name = "Password")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "It's necessary a pass")]
    [DataType(DataType.Password)]
    [MinLength(6, ErrorMessage = "Min six letters")]
    public string pass { get; set; }

    [Display(Name = "Confirm pass")]
    [DataType(DataType.Password)]
    [Compare("pass", ErrorMessage = "The pass don't match")]
    public string check_pass { get; set; }

    [Display(Name = "Email")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "It's necessary an email")]
    [DataType(DataType.EmailAddress)]
    public string email { get; set; }
}

So, in the [HttpGet] the identification number is obtained by the program and the [HttpPost] the information is changed by the identification number, or so it is supposed to do.

[HttpGet]
    public ActionResult recoverPass()
    {
        return View();
    }

    [HttpPost]
    public ActionResult recoverPass(userPassRecover user)
    {    
        using(inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
        {
            var u = dc.user.Where(a => a.cedula == user.cedula).FirstOrDefault();

            dc.user.Remove(u);
            dc.user.Add(user);
            dc.SaveChanges();

            return View();
        }
    }

I really don't know how to make this work, any suggestions are appreciated

If anyone has the same problem:

    [HttpGet]
    public ActionResult recoverPass()
    {

        return View();
    }

    [HttpPost]
    public ActionResult recoverPass(userRecoverPass user)
    {
        using (inventarioEntitiesDBA dc = new inventarioEntitiesDBA())
        {
            var u = dc.user.Where(a => a.idnumber == user.idnumber)?.FirstOrDefault();

            if (u != null)
            {
                u.pass = user.newpass;
                dc.SaveChanges();
            }

            return RedirectToAction("login");
        }
    }

Where the ʻuserRecoverPass` object is the model where only the information is requested to perform the password change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM