Search code examples
c#asp.netsql-serverasp.net-mvcasp.net-identity

Updating user data with ApplicationUser class - ASP.NET Identity


I have an Edit page for users, which works fine for GET, but for POST AplicationUser object is not saved in database. I mention that some of the object fields are null, but it's passed to POST method successfully.

I tried to assign a value for each property corresponding to database column in case it's null with if statements, but that haven't solved the problem, no errors either..
I figured out that Updating user data - ASP.NET Identity second answer (JoshdeVries) from this post may be related to my case.

A photo with the Object in Debugging mode

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id,Name,Email,Password,ConfirmPassword,Roles,PhoneNumber")] ApplicationUser editUserModel)
    {
        if (ModelState.IsValid)
        {

            var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
            var manager = new UserManager<ApplicationUser>(store);
            var result manager.Update(editUserModel);
            if (!result.Succeeded) // This is false, so Update Failed!
            {
                Console.WriteLine(result.Errors.ToList());
                return View(editUserModel);
            } 
            store.Context.SaveChanges();
            return RedirectToAction("UsersList");
        }
        return View(editUserModel);
    }

And in View 3 input fields like this(I need to fill more fields but these 3 are for test):

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

<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.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" })
        </div>
    </div>

Solution

  • [P.S.] Solution that I applied so far and it's working fine:

    {
            if (editUserModel.UserName == null)
            {
                editUserModel.UserName = editUserModel.Email;
            }
            if (ModelState.IsValid)
            {
                db.Entry(editUserModel).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("UsersList");
            }
            return View(editUserModel);
    }
    

    For some reason it seems to me that 'UserName' is some kind of Key that is mandatory to indentify the user row.