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

发布于 2024-08-26 18:08:56 字数 1930 浏览 4 评论 0原文

我有一个 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后知后觉 2024-09-02 18:08:56

我的意思是,在看不到 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);
            }
        });

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);
            }
        });
回眸一笑 2024-09-02 18:08:56

$.ajax({
类型:“获取”,
url: "/Search/Search?section=" + 部分 + "&township=" + 城镇 + "&range=" + 范围,
数据类型:“html”,
成功:函数(结果){
$('#corners').html(结果);
}
});

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文