0

Good Morning|Evening

I have this code for updating records in controller

[HttpPost]
        public JsonResult Userupdate(UserProfile user)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Form is not valid! " +
                          "Please correct it and try again."
                    });
                }

                var usersC = from x in _db.UserProfiles where x.UserId == user.UserId select x;
                //int studentCount = usersC.Count();
                //List<UserProfile> students = usersC.ToList(); 
                UserProfile vb = new UserProfile();
                vb.UserId = user.UserId;
                vb.UserName = user.UserName;
                vb.IdentificationNumber = user.IdentificationNumber;
                vb.department = user.department;
                //_db.UserProfiles.Add(vb);
                _db.SaveChanges();

                return Json(new { Result = "OK"});
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

I have this code for deleting records in controller

[HttpPost]
        public JsonResult userdelete(int userId)
        {
            try
            {
                //_personRepository.DeletePerson(personId);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

And you see "repository" in comment because I did not use it in my project; I used link method.

I write the following code for connection with database

UsersContext _db = new UsersContext();

BUT I didn't get the right way to delete it from database.

NOTE::: I am using jTable to show data.

can you help me :_)

2 Answers 2

1

This is using entity framework? Unfortunately, this doesn't work in EF the way you would want it to work when writing a service or MVC api. That is, passing only the id.

EF can only remove the entity once it is loaded in context. That means either retrieving the entity and then removing it or creating a "shell" entity, attaching it, then doing the remove. (Alternate approach to the Remove method is to attach it and then set its state to deleted and then do SaveChanges. It's the same thing under the hood)

var userEntity = new UserProfile {UserId = userId};
_db.Users.Attach(userEntity);
_db.Users.Remove(userEntity);
_db.SaveChanges();
Sign up to request clarification or add additional context in comments.

Comments

0

That's great when you solve your problem swiftly :)

Yes I solved my question with "Jim Leonardo" help.

Thank you very much Mr Jim Leonardo.


the following code is for updating records, and it is working fine.

[HttpPost]
        public JsonResult Userupdate(UserProfile user)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Form is not valid! " +
                          "Please correct it and try again."
                    });
                }

                var usersC = from x in _db.UserProfiles where x.UserId == user.UserId select x;
                var UserToUpdate = usersC.First();
                UserToUpdate.UserId = user.UserId;
                UserToUpdate.UserName = user.UserName;
                UserToUpdate.IdentificationNumber = user.IdentificationNumber;
                UserToUpdate.department = user.department;
                _db.SaveChanges();

                return Json(new { Result = "OK"});
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

and the following code is for deleting records; it is working fine.

[HttpPost]
        public JsonResult userdelete(UserProfile user, int userId)
        {
            try
            {
                var usersC = from x in _db.UserProfiles where x.UserId == user.UserId select x;
                var UserToUpdate = usersC.First();
                UserToUpdate.UserId = user.UserId;
                UserToUpdate.UserName = user.UserName;
                UserToUpdate.IdentificationNumber = user.IdentificationNumber;
                UserToUpdate.department = user.department;
                _db.UserProfiles.Remove(UserToUpdate);
                _db.SaveChanges();
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

Thank you again Mr Jim Leonardo

PROBLEM SOLVED :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.