ASP.NET MVC - 当 DropDownList 更改时刷新 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>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的意思是,在看不到 PartialView 的情况下,一种选择是将 PartialView 包装在 div 中:
然后让 ajax 在控制器中调用一个操作,该操作将返回 PartialViewResult 并将结果加载到该 div 中:
I mean one option without seeing your PartialView is to wrap the PartialView in a div:
Then have your ajax call an action in your controller that will return a PartialViewResult and load the result into that div:
$.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);
}
});