简体   繁体   中英

How to return View without reloading complete form after saving data using Bootstrap modal popup in Asp.Net MVC

I have one textbox with autocomplete feature and one button.On clicking button modal popup gets open.Modal popup has two textbox fields namely ProductName and Categoryame and one button to save this information into database. I already have success in saving data using modal popup.But after that i am getting this output {"status":true,"message":"Successfully Saved."}.

But i want to return same initial view with textbox and button so that the saved product starts getting appearing in autocomplete textbox. My Controller's Code is:

[HttpPost]
    public ActionResult Create(Product prod)
    {
        string message = "";
        bool status = false;
        if (ModelState.IsValid)
        {
            db.Products.Add(prod);
            db.SaveChanges();
            status = true;
            message = "Successfully Saved.";
            //string url = Url.Action("CreateProduct", "Products");
            //return Json(new { success = true });
            return new JsonResult { Data = new { status = status, message = message } };
        }
        return PartialView("Create", prod);

    }

and my jquery code is:

function bindForm(dialog) {
        $('form', dialog).submit(function () {
            if ($("#formCrud").valid()) {
                $.ajax({
                    url: this.action,
                    type: this.method,
                    data: $(this).serialize(),
                    dataType: 'json',
                    success: function (result) {
                        if (result.success) {                                
                            alert(result.message);
                            $('#myModal2').modal('hide');

                        } else {
                            $('#myModalContent2').html(result);
                            bindForm(dialog);
                        }
                    }
                });
                return false;
            }
        });
    }

You must change this.

[HttpPost]
    public ActionResult Create(Product prod)
    {
        string message = "";
        bool status = false;
        if (ModelState.IsValid)
        {
            db.Products.Add(prod);
            db.SaveChanges();
            status = true;
            message = "Successfully Saved.";
            //string url = Url.Action("CreateProduct", "Products");
            //return Json(new { success = true });
            return new JsonResult { Data = new { status = status, message = message, success = true } };
        }
        return PartialView("Create", prod);

    }

There is no success property in the json object you are returning, so you never get to evaluate result.success.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM