在Ajax.BeginForm中传递UpdateTargetId不会替换目标 [英] Passing UpdateTargetId in Ajax.BeginForm not replacing the target

查看:55
本文介绍了在Ajax.BeginForm中传递UpdateTargetId不会替换目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ajax.BeginForm中,即使我指定了targetId(这是我的局部视图),它仍在替换完整视图.我想念什么吗?

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

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-控制器操作

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);
        }

推荐答案

请确保页面/布局中包含jquery.unobtrusive-ajax.js.

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.

这篇关于在Ajax.BeginForm中传递UpdateTargetId不会替换目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆