MVC3:如何正确调用Ajax来更新部分视图

时间:2011-09-06 22:34:08

标签: ajax asp.net-mvc-3 asp.net-mvc-partialview

我的控制器中有2个动作:

// GET: /Directory/
public ActionResult Index()
{
    ViewModels.DirectoryIndex model = new ViewModels.DirectoryIndex();
    return View(model);
}

// POST: /Directory/Filter/[viewModel]
[HttpPost]
public ActionResult Filter(ViewModels.DirectoryIndex viewModel)
{
    int distance = 0;
    string state = string.Empty;

    if (viewModel.SelectedDistance > 0)
        distance = viewModel.SelectedDistance;
    else if (viewModel.SelectedState > 0)
        state = viewModel.States.Where(a => a.Value == viewModel.SelectedState.ToString()).FirstOrDefault().Text;

    // TODO:  Add filter activities here...

    return PartialView("IndexPartial", viewModel);
}

我有一个View和一个PartialView(注意:我没有使用Razor!)

视图如下:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<RainWorx.FrameWorx.MVC.ViewModels.DirectoryIndex>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <div class="Column12">
        <div class="Shadow"></div>
        <h2 class="h2row"><%= Model.PageTitle %></h2>

        <% using (Ajax.BeginForm("Filter", new AjaxOptions { UpdateTargetId = "filteredList" })) {  %>
        <div class="searchDirectory">
            <label title="State">State:&nbsp;</label>
            <%= Html.DropDownListFor(a => a.SelectedState, Model.States, "-- Select One --", new { @id = "ddlStates" })%>
            &nbsp;&nbsp;-or-&nbsp;&nbsp;
            <label title="ZipCode">Zip Code within:&nbsp;</label>
            <%= Html.DropDownListFor(a => a.SelectedDistance, Model.Distance, "-- Select One --", new { @id = "ddlDistance" })%>
            <input type="text" id="myZip" name="myZip" />
            <input type="submit" value="Filter" />
        </div>
        <% } %>

        <div id="filteredList" class="businessCard">
            <% { Html.RenderPartial("IndexPartial", Model); } %>
        </div>

        <div style="height: 200px;"></div>
    </div>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
    <link type="text/css" href="Content/css/directory.css" rel="Stylesheet" />
    <script src="Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript" />
    <title><%= Model.PageTitle %></title>
</asp:Content>

PartialView看起来像:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<RainWorx.FrameWorx.MVC.ViewModels.DirectoryIndex>" %>

<% foreach (var d in Model.Dealers) { %>
<div class="businessCard Outline">
    <div class="businessCard Logo">
        <img src="<%= Url.Content("~/Content/Images/Directory/logo01_150wide.png") %>" alt="Logo" />
    </div>
    <div class="businessCard Info">
        <div class="businessCard Name"><%= d.Name %></div>
        <div class="businessCard Address"><%= d.Address %></div>
        <div class="businessCard City"><%= d.City %>, <%= d.State %>  <%= d.ZipCode %></div>
        <div class="businessCard Phone"><%= d.Phone %></div>
    </div>
</div>
<% } %>

现在,出于某种原因,当我选择要使用的过滤器并提交表单时,它会正确调用第二个操作。但是,它不会刷新PartialView,而是将PartailView渲染为完整视图。在网址中:

  1. 我从http://mysite.com/Directory开始 - 选择我的过滤器,点击提交。
  2. 当我希望在http://mysite.com/Directory/Filter !!
  3. 结束时,我以http://mysite.com/Directory结束

    我显然缺少一些简单的东西。我之前在Razor中做过这件事(喜欢Razor在这个混乱的BTW上),这一切都在那里工作。

    注意:这是我推出的第三方产品,所以不要把所有东西都转换成Razor。

1 个答案:

答案 0 :(得分:0)

您需要在html中引用以下两个文件。我认为有一个jQuery替代方案......但是引用这两个JavaScript文件(可能已经在你的MVC解决方案中)应该可以解决你的问题。

<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>