Question

I am using SimpleMembership in an MVC4 web app. I can't figure out how to edit the profile information. I thought I could do it just as you do any other table.

        [HttpPost]
        public ActionResult EditUser(UserProfile user)
        {
            if (ModelState.IsValid)
            {
                udb.Entry(user).State = EntityState.Modified;
                udb.SaveChanges();
                return RedirectToAction("Index");
            }

But I get an error saying entity state does not exist in the current context. My context is defined as follows at the top of the controller.

private UsersContext udb = new UsersContext();

I can find plenty of references on access profile data but nothing for editing the data. How can I save the edited UserProfile data back to the db?

EDIT: I was able to resolve the entityState error -- I had to include system.data and system.data.entity. However now when I run I get an error on edit which says unexpected number of rows modified (0). and points to the udb.SaveChanges() line. Still can't figure out how to modify UserProfile data elements.

Was it helpful?

Solution

Simple answer. I needed to set all the fields for the Model in my view. I was only allowing the user to change 4 out of the 6 so two were not being set.

I thought when you pass the model to the view, the view would pass the same field values onto the action if they were not set in the view. For example: if I set FirstName in the view but not UserName the original UserName that was sent to the view would be passed to the Model. That appears not to be the case. For all the items in the model I did not allow them to change in the view I had to set hidden fields to set the fields so a complete model was sent.

It may be better to set individual fields but I am not sure how to do that yet and that was not the question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top