Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - Refresh PartialView when DropDownList changed

I have a search form that is an Ajax form. Within the form is a DropDownList that, when changed, should refresh a PartialView within the Ajax form (via a GET request). However, I'm not sure what to do in order to refresh the PartialView after I get back my results via the GET request.

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>
like image 518
Bryan Roth Avatar asked Mar 25 '10 16:03

Bryan Roth


1 Answers

I mean one option without seeing your PartialView is to wrap the PartialView in a div:

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

Then have your ajax call an action in your controller that will return a PartialViewResult and load the result into that div:

$.ajax({
            type: "GET",
            url: "Search/Search?section=" + section + "&township=" + township + "&range=" + range,
            dataType: "html",
            success: function (result) {
                $('#corners').html(result);
            }
        });
like image 181
amurra Avatar answered Sep 22 '22 16:09

amurra