ASP.NET MVC - 当下拉列表更改时刷新PartialView

6

我有一个搜索表单,它是一个Ajax表单。在表单中有一个DropDownList,当改变时,应该通过GET请求在Ajax表单内刷新PartialView。然而,我不知道在收到GET请求的结果后如何刷新PartialView。

Search.aspx

<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Search
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<script type="text/javascript">
    $(document).ready(function () {
        $("#Sections").change(function () {

            var section = $("#Sections").val();
            var township = $("#Townships").val();
            var range = $("#Ranges").val();

            $.ajax({
                type: "GET",
                url: "Search/Search?section=" + section + "&township=" + township + "&range=" + range,
                contentType: "application/json; charset=utf-8",
                dataType: "html",
                success: function (result) {
                    // What should I do here to refresh PartialView?
                }
            });
        });

    });
</script>

    <h2>Search</h2>

    <%--The line below is a workaround for a VB / ASPX designer bug--%>
    <%=""%>
    <% Using Ajax.BeginForm("Search", New AjaxOptions With {.UpdateTargetId = "searchResults", .LoadingElementId = "loader"})%>        
        Township <%= Html.DropDownList("Townships")%>
        Range <%= Html.DropDownList("Ranges")%>
        Section <%= Html.DropDownList("Sections")%>

        <% Html.RenderPartial("Corners")%>

        <input type="submit" value="Search" />        
        <span id="loader">Searching...</span>
    <% End Using%>
    <div id="searchResults"></div> 

</asp:Content>

你的局部视图的HTML是什么? - amurra
2个回答

7

我的意思是,如果没有看到你的PartialView,一种选择是将PartialView包装在一个div中:

<div id="corners"><% Html.RenderPartial("Corners")%></div>

然后,您的ajax调用控制器中的一个操作,该操作将返回PartialViewResult并将结果加载到该div中:
$.ajax({
            type: "GET",
            url: "Search/Search?section=" + section + "&township=" + township + "&range=" + range,
            dataType: "html",
            success: function (result) {
                $('#corners').html(result);
            }
        });

1
非常感谢您的建议。我稍微调整了一下,现在它可以工作了。 - Bryan Roth

0

$.ajax({ type: "GET", url: "/Search/Search?section=" + section + "&township=" + township + "&range=" + range, dataType: "html", success: function (result) { $('#corners').html(result); } });

$.ajax({ type: "GET", url: "/Search/Search?section=" + section + "&township=" + township + "&range=" + range, dataType: "html", success: function (result) { $('#corners').html(result); } });


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接