Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing UpdateTargetId in Ajax.BeginForm not replacing the target

In Ajax.BeginForm even though I have specified the targetId which is my partial view, it is replacing the full view. Am I missing something ?

ProductList.cshtml

<div class="form-style-1" style="float:left">
           @using (Ajax.BeginForm("SearchProducts", "Product", new AjaxOptions()
           {
           HttpMethod="POST",
           InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
           UpdateTargetId = "product-list-container"
           }))
           {
           <form>
              <input name="SearchText" id="SearchText" class="field-divided" style="width: 300px;" type="text" />
              <input type="submit" value="Search" class="myButton" />
           </form>
           }
        </div>@Html.Partial("ListPartialView", Model)


ListPartialView.cshtml - 

<div id="product-list-container">
    @foreach (var product in Model)
    {
        <div class="product_box margin_r40">
            <div class="thumb_wrapper"><a><img src="@Url.Content(@product.ImagePath)" alt="image 1" /></a></div>
            <h3>@product.ProductName</h3>
            <p>@product.Description</p>
        </div>
    }
    <div class="button_01"><a href="#">View All</a></div>
    <div class="cleaner"></div>
</div>

SearchProducts - Controller action

[HttpPost]
        public ActionResult SearchProducts()
        {
            var searchTxt = Request.Form["SearchText"];                                                
            IEnumerable<Product> searchedProducts = (from p in dal.Products
                                                     where p.ProductName.Contains(searchTxt) ||
                                                     p.Description.Contains(searchTxt)
                                                     select p).ToList();

            return PartialView("ListPartialView", searchedProducts);
        }
like image 747
Abhilash V Avatar asked Dec 24 '22 02:12

Abhilash V


1 Answers

Make sure you have the jquery.unobtrusive-ajax.js included in your page/layout.

<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

This file make your form ajaxified.

If you do not have this file included, your form will be a normal form. Clicking on the submit button will do a normal form submit which might be the cause for you to see the entire page.

like image 162
Shyju Avatar answered Apr 19 '23 23:04

Shyju