ASP.NET MVC - DropDownList更改时刷新PartialView

时间:2010-03-25 16:54:00

标签: jquery ajax asp.net-mvc asp.net-mvc-2 asp.net-mvc-partialview

我有一个Ajax表单的搜索表单。在表单中有一个DropDownList,当更改时,应该刷新Ajax表单中的PartialView(通过GET请求)。但是,我不确定在通过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>

2 个答案:

答案 0 :(得分: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 :(得分:0)

$。AJAX({             类型:“GET”,             url:“/搜索/搜索?section =”+ section +“&amp; township =”+ township +“&amp; range =”+ range,             dataType:“html”,             成功:功能(结果){                 $( '#角落')HTML(结果);             }         });