MVC如何从下拉列表中刷新网格?

时间:2010-09-13 13:10:50

标签: asp.net-mvc

当我在下拉列表中更改年份时,如何刷新下面的网格?

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/AdminAccounts.master" Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.BankHolidayViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    BankHoliday
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="AdminAccountsContent" runat="server">
<h3>Bank Holiday Administration</h3>
<p>Select the year: <%: Html.DropDownListFor(model => model.SelectedYear, Model.YearList)%></p>
<table>
    <tr>
        <th>Bank Holiday</th>
        <th>Date</th>
        <th>Notes</th>
    </tr>
    <% foreach (var bankHolidayExtended in Model.BankHolidays)
    { %>
        <% Html.RenderPartial("BankHolidaySummary", bankHolidayExtended); %>
    <% } %>
</table>

1 个答案:

答案 0 :(得分:1)

最简单的方法是创建一个标记,该标记将成为从AJAX调用中显示网格的目标:

<div id="bankHolidays"></div>

然后你就是jQuery看起来像这样:

$(function() {
    $("#SelectedYear").change(function() {
        var year = $("#SelectedYear").val();
        $("#bankHolidays").load("/YourController/BankHolidays/" + year);
    });
});

然后你只需创建一个名为BankHolidays的控制器动作:

public ActionResult BankHolidays(int year)
{
  this.ViewData.Model = repository.GetBankHolidaysForYear(year);
  return this.View();
}

最后,在你的BankHolidays.ascx中你从那里移动你的foreach循环,那个BankHolidays.ascx的模型是你的IEnumerable。这个HTML片段将在AJAX调用中返回。 jQuery会将HTML设置为“bankHolidays”标记内的HTML。