开发者

MVC3: How to correctly call Ajax to update PartialView

I have 2 Actions in my controller:

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

I have a View and a PartialView (NOTE: I am not using Razor!)

The view looks like:

<%@ 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.SelectedDi开发者_运维技巧stance, 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>

The PartialView looks like:

<%@ 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>
<% } %>

Now, for some reason, when I select a filter to use and submit the form it does call into the second action correctly. However, it does not refresh the PartialView, instead I get the PartailView rendered as a full view. In URL terms:

  1. I start at http://mysite.com/Directory - select my filter, click submit.
  2. I end at http://mysite.com/Directory/Filter when I expect to end at http://mysite.com/Directory !!

I am obviously missing something simple. I've done this before in Razor (love Razor over this mess BTW) and it all works there.

NOTE: This is a 3rd party product I am extending so don't have the luxury of converting everything to Razor.


You need to have both the following files referenced in your html. I think there is a jQuery alternative... but referencing both of these JavaScript files (which are probably already in your MVC solution) should solve your problem.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜