Thumb

Part-10: Point of Sale(POS) User Creation & user registration using ASP.NET MVC | Jquery | Ajax

9/1/2020 12:00:00 AM

Step-1:

Follow the previous video and articles to complete the POS tutorials

  • Please go to Home controller
  • We already worked in previous article UserCreate view so now Add this code to below of “UserCreate.cshtml” view
<script>
    function SaveUser() {
        var user = new Object();
        user.Username = $("#exampleInputUser").val();
        user.Password = $("#exampleInputPassword").val();
        user.Role = $("#role").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) {
                    alert("Save Success!");
                }

                else {
                    alert("Save failed!");
                }

            },

            error: function () {

                alert("Error!")

            }

        });

    }
</script>

Step-2:

  • Paste below code to HomeController
        [HttpPost]
        public JsonResult SaveUser(User user)
        {
            POS_TutorialEntities db = new POS_TutorialEntities();
            bool isSuccess = true;
            try
            {
                user.Status = 1;
                user.Password = AppHelper.GetMd5Hash(user.Password);
                db.Users.Add(user);
                db.SaveChanges();
            }
            catch (Exception)
            {
                isSuccess = false;
            }

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

About Teacher

Reza Karim

Software Engineer

More about him