jquery - ASP.NET MVC - DropDownList 更改时刷新部分 View

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

我有一个 Ajax 表单的搜索表单。表单内有一个 DropDownList,当更改时,应刷新 Ajax 表单内的 PartialView(通过 GET 请求)。但是,在通过 GET 请求返回结果后,我不确定如何刷新 PartialView。

搜索.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>

最佳答案

我的意思是,在看不到 PartialView 的情况下,一种选择是将 PartialView 包装在 div 中:

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

然后让您的 ajax 在 Controller 中调用一个操作,该操作将返回 PartialViewResult 并将结果加载到该 div 中:

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

关于jquery - ASP.NET MVC - DropDownList 更改时刷新部分 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2517587/

相关文章:

jquery - 将 Json 数据传递给 MVC Controller - Action 方法对象参数

javascript - 在 Knockout 中使用 ckeditor 作为 observableArray 的一部分

javascript - Kendo -MVC ListView - 如何获取所选行上的复选框状态(选中或未选中)

javascript - 使用 AJAX 加载异地内容

javascript - 获取上一个可见元素jquery

javascript - ajax值未返回

javascript - 上传图像 - 它们中是否有域名 - chrome 不发送 session ID

c# - Entity Framework : many to many relationship tables

javascript - jQuery .each() 方法的两个参数的解释

javascript - 如何通过单击按钮调整div的大小并使其变为全屏