Abhilash V Abhilash V - 10 months ago 85
HTML Question

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 ?


<div class="form-style-1" style="float:left">
@using (Ajax.BeginForm("SearchProducts", "Product", new AjaxOptions()
InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
UpdateTargetId = "product-list-container"
<input name="SearchText" id="SearchText" class="field-divided" style="width: 300px;" type="text" />
<input type="submit" value="Search" class="myButton" />
</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>
<div class="button_01"><a href="#">View All</a></div>
<div class="cleaner"></div>

SearchProducts - Controller action

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

return PartialView("ListPartialView", searchedProducts);


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.