MVC3:如何正确调用Ajax更新PartialView

发布于 2024-12-03 06:49:42 字数 4037 浏览 4 评论 0 原文

我的控制器中有 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 渲染为完整视图。在 URL 方面:

  1. 我从 http://mysite.com/Directory 开始 - 选择我的过滤器,单击提交。
  2. 当我希望在 http://mysite.com/Directory/Filter 结束="http://mysite.com/Directory" rel="nofollow">http://mysite.com/Directory!

我显然缺少一些简单的东西。我以前在 Razor 中做过这个(顺便说一句,我喜欢 Razor 而不是这个烂摊子),而且一切都在那里工作。

注意:这是我正在扩展的第 3 方产品,因此没有将所有内容转换为 Razor 的奢侈。

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: </label>
            <%= Html.DropDownListFor(a => a.SelectedState, Model.States, "-- Select One --", new { @id = "ddlStates" })%>
              -or-  
            <label title="ZipCode">Zip Code within: </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>

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心作怪 2024-12-10 06:49:42

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

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文