在RenderPartial View中从Ajax.BeginForm创建的Ajax.ActionLink

时间:2013-03-05 20:13:28

标签: javascript ajax asp.net-mvc razor unobtrusive-javascript

我想确认这个限制是设计还是我做错了:

我有两个RenderPartials的视图:

@model Heelp.ViewModels.CompanyIndexViewModel

@{ Html.RenderPartial(MVC.Company.Views.IndexSearch, Model.SearchViewModel); }
@{ Html.RenderPartial(MVC.Company.Views.IndexMap, Model.MapViewModel); }

在第一个部分视图中,我有一个Ajax.BeginForm:

@model Heelp.ViewModels.CompanyIndexSearchViewModel

@using (Ajax.BeginForm(MVC.Company.CategoryGetAllBySearch(), new AjaxOptions { UpdateTargetId = "searchCompanyResults", InsertionMode = InsertionMode.Replace }, new { @id = "searchBoxWrap" }))
{
  @Html.AntiForgeryToken()

  @Html.HiddenFor(m => m.IsCenterFromUser)
  @Html.HiddenFor(m => m.CenterLat)
  @Html.HiddenFor(m => m.CenterLng)
  @Html.HiddenFor(m => m.Zoom)
  @Html.HiddenFor(m => m.SearchRadius)

  @Html.TextBoxFor(m => m.Search, new { @placeholder = @HeelpResources.CompanyIndexViewSearchPlaceholder })
  <input type="button" value="«" id="clearKeywords"/>
  @Html.TextBoxFor(m => m.Location, new { @placeholder =   @HeelpResources.CompanyIndexViewLocationPlaceholder })
  <input type="button" value="«" id="clearLocation"/>
  <input type="button" value="X" id="hereButton"/>
  <input type="submit" value="@HeelpResources.CompanyIndexViewSearchButtonLabel"/>
}
<div id="searchCompanyResults" class="clearfix" style="z-index: 10; position: absolute; width: 400px;"></div>

Ajax.BeginForm在searchCompanyResults div中生成一个PartialView,其中包含一个Ajax.ActionLink列表:

   @model Heelp.ViewModels.CategoryGetAllBySearchListViewModel

<p class="float-left margin-top align-left"><span>Encontrámos <em><a href="#">@Model.TotalSearchCount</a></em> resultados nas categorias:</span></p>
<div class="clear-both">
    <div id="searchResultsList" class="float-left">
        <ul>
            @foreach (var item in Model.CategoryGetAllBySearch)
            {
                <li>
                    @Ajax.ActionLink(
                        String.Format("{0} {1} ver »", item.SearchCount, item.Name), 
                        MVC.Company.GetAllByCategory(item.Id, Model.Search, Model.Location, Model.IsCenterFromUser, Model.CenterLat, Model.CenterLng, Model.SearchRadius), 
                        new AjaxOptions { OnBegin = "CompanyGetAllByCategoryOnBegin(" + item.Id + ")", OnSuccess = "CompanyGetAllByCategoryOnSuccess" })
                </li>
            }        
        </ul>
    </div>
</div>

这里的问题是,如果我没有包含“&lt; script src =”〜/ Scripts / jquery.unobtrusive-ajax.min.js“&gt;”的链接在PartialView中,Action.Link返回Json文本。

编辑:我检测到的是,当我点击Action.Link时,第一次提交2次,第二次提交4次,并且一直在增长,为什么? 我必须这样做吗?

1 个答案:

答案 0 :(得分:0)

如果您想使用Ajax.BeginFormAjax.ActionLink以及Ajax中的其他人,则应在您的布局中包含jquery.unobtrusive-ajax.js文件。它包含拦截点击链接和通过取消操作提交表单的代码,并通过AJAX进行。

您不需要在部分视图中包含该文件两次。

相关问题