0

I'm currently working on user account and so far i've managed to work upon register and login panel with email confirmation process. Here i'm stuck in forget password option. I'm using ado.net entity framework. All i have to do is to change password for the registered email. This is what i've done so far.

Note: getting the error in controller action method

the entity type is not part of the model for the current context

Edit

I have a table named registration attached to the DBContext class. And i'm trying to update the records (particularly the password field) for the forger password option. I made the property class UpdateAcccount.cs with validation as attached below. In order to update the password, I retrieved the row matching with email id. And then transferring the updated password in the database.

This time i'm getting the error of "password does not match" although there's no field of confirm password in the database(registration table) + i even tried to use bind(exclude) attribute for confirm password but that didn't work either.

Controller class

[HttpPost]

    public ActionResult UpdateAccount(UpdateAccount account)
    {
        string message = "";
        bool status = false;

        if (ModelState.IsValid)
        {
            account.Password = Crypto.Hash(account.Password);
            account.ConfirmPassword = Crypto.Hash(account.ConfirmPassword);

            using (TravelGuide1Entities entity = new TravelGuide1Entities())
            {
                try
                {
                    var v = entity.Registrations.Where(a => a.Email == account.Email).FirstOrDefault();

                    if (v != null)
                    {
                        v.Password = account.Password;

                        entity.Entry(v).State = System.Data.Entity.EntityState.Modified;

                        entity.SaveChanges();

                        return RedirectToAction("Login");
                    }
                }
                catch(Exception e)
                {

                }
            }
        }

            return View();
    }

UpdateAccount.cs

public class UpdateAccount
{
    [Display(Name = "Email")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Email id required")]
    public string Email { get; set; }

    [Display(Name = "New Password")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Password required")]
    [DataType(DataType.Password)]
    [MinLength(6, ErrorMessage = "Minimum 6 character required")]
    public string Password { get; set; }

    [Display(Name = "Confirm Password")]
    [DataType(DataType.Password)]
    [Compare("Password", ErrorMessage = "Password do not match")]
    public string ConfirmPassword { get; set; }
}

UpdateAccount.cshtml

@model Travel.Models.UpdateAccount

@{
    ViewBag.Title = "UpdateAccount";
}

<h2>UpdateAccount</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>UpdateAccount</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
Salar Muhammad
  • 334
  • 1
  • 8
  • 21
  • Possible duplicate of [Getting "The entity type is not part of the model for the current context."](https://stackoverflow.com/questions/22394603/getting-the-entity-type-model-is-not-part-of-the-model-for-the-current-contex) – Igor Aug 28 '17 at 16:11
  • You need to attach the entity OR retrieve the entity and modify the retrieved entities values based on the incoming model's values. See the marked duplicate. – Igor Aug 28 '17 at 16:11
  • Added this line to retrieve the record from database var v = entity.Registrations.Where(a => a.Email == account.Email).FirstOrDefault(); yet getting the same error – Salar Muhammad Aug 28 '17 at 16:24
  • You do not seem to understand. The error stems from line `entity.Entry(account)`. Either: 1) `UpdateAccount` is not a type in your `DbContext` models OR 2) It is a type but you still have to retrieve the instance first OR attach this instance to the DbContext. I recommend an EF tutorial so you understand how you should structure your insert/update code. – Igor Aug 28 '17 at 18:09
  • I got your point. Let me explain what's happening now. Please see the updated question. – Salar Muhammad Aug 29 '17 at 07:04

0 Answers0