6

我有一個Ajax表單的搜索表單。表單中有一個DropDownList,在更改時應該刷新Ajax表單中的PartialView(通過GET請求)。但是,我不確定在通過GET請求取回結果後,如何刷新PartialView。ASP.NET MVC - 在DropDownList更改時刷新部分視圖

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> 
+0

什麼是你的局部視圖的HTML? – amurra 2010-03-25 17:10:41

回答

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

非常感謝您的建議。我做了一些調整,並使其工作。 – 2010-03-25 18:23:23

0

$阿賈克斯({ 類型: 「GET」, URL: 「/搜索/搜索部分=」 +部分+ 「&鄉=」 +鄉鎮+ 「&範圍=」 +範圍, 數據類型:「HTML 「, 成功:function(result){('#corners')。html(result); } });