0

I submit a form post in Asp.Net MVC Partial View with JQuery Ajax.

The Partial View is inside one of the tabs in bootstrap tabs (there is 4 tabs and each tab has its Partial View). And there is a View, which covers all these tabs. And there is a Layout which covers all.

This is Partial view Action method call inside the cover View:

 @Html.Action("AddProductPartial", "Products")

Partial View Action:

 [HttpPost]
        public ActionResult AddProductPartial(ProductCategoryBrand pcb)

        {
            if (ModelState.IsValid)
            {
                Product p = new Product();
                p.CategoryID = Convert.ToInt32(pcb.CategoryViewModel.Kategori);
                p.BrandID = Convert.ToInt32(pcb.BrandViewModel.BrandName);
                p.ExchangeName = pcb.ExchangeViewModel.ExchangeName;

               ProductRepository prep = new ProductRepository();
                prep.AddProduct(p)
            }
            loadBrands();
            getRates();

            return View(pcb);

        }

JQuery Ajax:

$('#saveProduct').click(function () {

    $.ajax({
        url: "/Products/AddProductPartial",
        type: 'POST',          
        data: "{'CategoryID':" + categoryID + ", 'BrandID':" + brandID + ",'ExchangeName':" + exchangeName + "}",
        async:true,            
        contentType: "application/json; charset=utf-8",
        dataType: "json",         
        success: function (data) {
            alert("success");
        },
        error: function (xhr, status, error) {
            alert(xhr.responseText)

        }
    });
});

These statements below populates dropdownlisfor from db. I added these methods because dropdownlistfor in the partial view was giving null exception after Ajax submit.

loadBrands();
 getRates();

After adding these statements, the problem occured.

After submit, Partial view is rendering weird: Bootstrap nav-tabs are no longer visible. Because the View that covers the Partial view is not rendering at all. When I change this line:

 return View(pcb);

to

return Partial View(pcb);

Then renders only the Partial view itself, independently from the all layout.

Could you please help. Thanks.

5
  • Can I see the ajax code? Perhaps on the success callback for the ajax function, you are replacing the entire web page instead of just HTML element that contains the partial. Commented Jun 13, 2017 at 18:23
  • I added JQuery Ajax code. @ChristopherMMiller I shortened the parameters because code seems too long here Commented Jun 13, 2017 at 18:41
  • you should probably replace @Html.Action("AddProductPartial", "Products") with @Html.Partial("AddProductPartial") and your public ActionResult AddProductPartial should just return a Json object with helpful information like Success/Error/New Product ID etc Commented Jun 13, 2017 at 19:21
  • @JamieD77 I tryed @Html.Partial("AddProductPartial") but this way action method (Get) was not calling. And when I write something like return Json(new { data = 1 }, JsonRequestBehavior.AllowGet); this line results in empty page writing only {"data":1} Commented Jun 13, 2017 at 20:36
  • You are returning return View(pcb); Replace with json value data i.e Commented Jun 14, 2017 at 5:23

1 Answer 1

1

Here is the simplest example of partial view:

public class HomeController : Controller
{
    [HttpPost]
    public PartialViewResult GetXlFile()
    {
        return PartialView("_ExcelGrid");
    }

    public ActionResult GetXlFile(int? id)
    {
        return View();
    }

_ExcelGrid.cshtml in shared:

A Partial View Info

View:

<!DOCTYPE html>
@*credit to
    https://stackoverflow.com/questions/5410055/using-ajax-beginform-with-asp-net-mvc-3-razor*@
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index800</title>
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#pvButton').click(function (event) {
                $.ajax({
                    url: this.action,
                    type: "POST",
                    data: $(this).serialize(),
                    success: function (result) {
                        $('#result').html(result);
                    }
                });
                return false;
            });
        });
    </script>
</head>
<body>
    <form>
        <div>
            <input id="pvButton" type="button" value="OK" />
            <div id="result"></div>
        </div>
    </form>
</body>
</html>
Sign up to request clarification or add additional context in comments.

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.