6

J'ai un formulaire de recherche qui est un formulaire Ajax. Dans le formulaire se trouve un DropDownList qui, lorsqu'il est modifié, doit actualiser une vue partielle dans le formulaire Ajax (via une requête GET). Cependant, je ne suis pas sûr de ce que je dois faire pour actualiser la vue partielle après avoir récupéré mes résultats via la requête GET.ASP.NET MVC - Refresh PartialView lorsque DropDownList a été modifié

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

Quel est le code html de votre vue partielle? – amurra

Répondre

7

Je veux dire une option sans voir votre PartialView est d'envelopper le PartialView dans un div:

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

ont ensuite votre appel ajax une action dans votre contrôleur cela retournera un PartialViewResult et chargera le résultat dans ce div:

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

Merci beaucoup pour cette suggestion. J'ai fait un peu de peaufinage et je l'ai mis au travail. –

0

.ajax $ ({ Type: "GET", url: "/ Recherche/Recherche section =" + section + "& canton =" + canton + "& range =" + gamme, dataType: « html ", succès: fonction (résultat) { $ ('# coins'). Html (résultat); } });

Questions connexes