0

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>
  • If those are the only three values you've bound to your view, you aren't posting enough information for EF to find the matching entity. – Tieson T. May 30 '17 at 23:48
  • How to Fix that ? Email address is unique for sure, EF should be able to find it. –  May 30 '17 at 23:58
  • Only if that field is marked as the primary key field. The correct way to "fix" this is to not use data models as view models. – Tieson T. May 30 '17 at 23:59
  • manager.Update(editUsersModel); will work only with an object of type ApplicationUser. Making another model just to copy each Property of ApplicationUser object to my Model object and making a query for each of the model properties to change value in database, I find it to be bad design.. :) –  May 31 '17 at 00:09
  • "making a query for each of the model properties to change value in database" - I'm not sure what you mean by that. Assuming your view model had your primary key field (say, `ID`), you would do something like `var user = context.Users.SingleOrDefault(u => u.ID == model.ID)`, and then update the relevant fields. Regardless, I would assume you just need to add a hidden input field for your primary key field. – Tieson T. May 31 '17 at 00:36
  • I noticed that when I use if (!result.Succeeded) { Console.WriteLine(result.Errors.ToList()); return View(editUserModel); } Succeeded is false... –  May 31 '17 at 09:32
  • In short... you need to include ID in your view as a hidden field so it can be posted back to your controller – Nick.Mc May 31 '17 at 10:22
  • Hey Nick.McDermaid, thanks for answer, I get it now, it needs an identifier, I used another syntax to solve this, I updated the Topic. :) –  May 31 '17 at 11:24

1 Answers1

0

[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.