Thumb

Part-12: Point of Sale(POS) Update user using ASP.NET MVC | Jquery | Ajax

9/6/2020 12:00:00 AM

Step-1

Post your any code related problem to www.abctutorial.com

      Follow the previous video and articles to complete the POS tutorials

  • Please go to UserCreate.cshtml view.
  • We already worked in previous article UserCreate.cshtml view so now add this code inside your form.
<input type="hidden" id="exampleInputUserId" />
<input type="hidden" id="status" />
  • Update SaveUser() method inside the Javascript by below code
<script>
    function SaveUser() {
        var user = new Object();
        user.UserId = $("#exampleInputUserId").val();
        user.Username = $("#exampleInputUser").val();
        user.Password = $("#exampleInputPassword").val();
        user.Role = $("#role").val();
        user.Status = $("#status").val();
        var data = JSON.stringify({
            user: user
        });
        return $.ajax({
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            type: 'POST',
            url: "/Home/SaveUser",
            data: data,
            success: function (result) {
                if (result == true) {
                    GetAllUser();
                    Reset();
                    alert("Save Success!");
                }
                else {
                    alert("Save failed!");
                }
            },
            error: function () {
                alert("Error!")
            }
        });
    }
</script>
  • Add below new JS method to your javascript
<script>
    function Edit(UserId, Username, Password, Role, Status) {
        $("#exampleInputUserId").val(UserId);
        $("#exampleInputUser").val(Username);
        $("#exampleInputPassword").val(Password);
        $("#exampleInputPasswordRetype").val(Password);
        $("#role").val(Role);
        $("#status").val(Status);
        $("#exampleInputPassword").hide(); 
        $("#exampleInputPasswordRetype").hide();
    }
    function Reset() {
        $("#exampleInputUserId").val(null);
        $("#exampleInputUser").val(null);
        $("#exampleInputPassword").val(null);
        $("#exampleInputPasswordRetype").val(null);
        $("#role").val(null);
        $("#status").val(null);
        $("#exampleInputPassword").show();
        $("#exampleInputPasswordRetype").show();
    }
</script>

Step-2

  • Update “SaveUser” inside the HomeController method by below code
        [HttpPost]
        public JsonResult SaveUser(User user)
        {
            POS_TutorialEntities db = new POS_TutorialEntities();
            bool isSuccess = true;

            if (user.UserId>0)
            {
                db.Entry(user).State = EntityState.Modified;
            }
            else
            {
                user.Status = 1;
                user.Password = AppHelper.GetMd5Hash(user.Password);
                db.Users.Add(user);
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception)
            {
                isSuccess = false;
            }

            return Json(isSuccess, JsonRequestBehavior.AllowGet);
        }
  • Now run the project.
  • Then save data.

About Teacher