【问题标题】:Ajax.ActionLink created from Ajax.BeginForm in a RenderPartial View在 RenderPartial 视图中从 Ajax.BeginForm 创建的 Ajax.ActionLink
【发布时间】:2013-02-20 09:48:48
【问题描述】:

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

我有一个带有两个 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>

这里的问题是,如果我在 PartialView 中不包含指向“”的链接,Action.Link 会返回 Json文本。

编辑: 我发现当我点击 Action.Link 时,第一次提交了 2 次,第二次提交了 4 次,并且不断增长,为什么? 我必须这样做吗?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 您好,谢谢。如果是在调用RenderPartial的View中,则不起作用,只能在列出Ajax.Actions的Partial View中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多