【问题标题】:Ajax.BeginForm replaces whole page onchange of a dropdownlistAjax.BeginForm 替换下拉列表的整个页面
【发布时间】:2012-05-12 19:53:19
【问题描述】:

目的

我有一个简单的表格列出名称(在部分视图中),在它上面有一个包含这些名称的下拉列表。目的是根据下拉列表中选择的名称过滤表。一旦下拉列表中的选定值发生更改,过滤就会发生,并且应该只再次呈现部分视图。

问题

当我在下拉列表中选择一个值时,部分视图不会在另一个视图中呈现,而是显示为整个页面。 但是,当我在我的 Ajax.BeginForm 块中包含一个提交按钮并触发提交按钮上的操作时,它确实按预期工作......

代码

控制器

public PartialViewResult Filter(string filterName) {
    var names = from p in db.People
                select p;

    if (!String.IsNullOrEmpty(filterName))
    {
        names = names.Where(p => p.Name.Equals(filterName));
    }

    return PartialView("_PersonsTable", names);
}

查看

@model IEnumerable<Sandbox.Models.Person>

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@using (Ajax.BeginForm("Filter", "Person", new AjaxOptions { 
    HttpMethod = "Get", UpdateTargetId = "SearchResults", InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace }))
{
    @Html.DropDownList("filterName", new SelectList(ViewBag.Names), "Select a name", new   { onchange = "this.form.submit()" })
}

@Html.Partial("_PersonsTable")

局部视图

@model IEnumerable<Sandbox.Models.Person>

<table id="SearchResults">
    <tr>
        <th>
            Name
        </th>
        <th>
            Age
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Age)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}
</table>

那么为什么我的 searchResults 表没有呈现为部分视图?

我的 _Layout 视图中包含这些脚本:

<script src="/Scripts/jquery-1.7.2.js" type="text/javascript"></script>
<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>

【问题讨论】:

    标签: ajax asp.net-mvc razor


    【解决方案1】:

    在您的下拉列表中替换:

    new { onchange = "this.form.submit()" }
    

    与:

    new { onchange = "$(this.form).submit();" }
    

    同时摆脱所有MicrosoftAjax*.js 脚本。这些是完全遗留的,不应在 ASP.NET MVC 3 和更新的应用程序中使用。如果您从旧版本迁移,它们仅出于兼容性原因而提供。 jQuery.jsjquery.unobtrusive-ajax.js 就足够了。

    【讨论】:

    • 这真的很愚蠢..我只为这个大声笑浪费了 2 个小时。谢谢
    【解决方案2】:

    不确定我是否理解正确,但如果提交按钮可以正常工作,为什么不这样做:

    @Html.DropDownList("filterName", new SelectList(ViewBag.Names), "Select a name", new   { onchange = "clickMe('mybuttonId')" })
    

    写小脚本

    function clickMe(id) {
      $('#' + id).click() // where id is id of button that should submit the form.
    }
    

    如果您不想显示按钮,则只需将其隐藏即可。这是一个 hack,如果它不符合您的需求,我们可以进一步调查。

    【讨论】:

      猜你喜欢
      • 2015-07-10
      • 2014-06-25
      • 1970-01-01
      • 2011-06-25
      • 1970-01-01
      • 2014-07-25
      • 2013-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多