控制网格的MVC下拉列表 - 使这个工作的最佳方法是什么?

时间:2010-10-26 15:39:47

标签: asp.net-mvc

我在客户端有一个下拉列表。当用户进行选择时,我的jquery脚本会提取选择的新值。 但下面的代码不起作用,因为我无法弄清楚如何将选定的值发送回控制器。我不确定发送参数的语法,因为我正在使用分页参数。也许我应该做一个Ajax调用呢?

视图看起来像;

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

<asp:Content ID="Content2" ContentPlaceHolderID="AdminAccountsContent" runat="server">
    <% using (Html.BeginForm())
       {%>
       <%: Html.AntiForgeryToken() %>
        <h3>Bank Holiday Administration</h3>
        <p>Select the year: <%: Html.DropDownListFor(model => model.SelectedYear, Model.YearList)%></p>
            <fieldset>
            <legend>Enter the bank holidays here:</legend>
            <table>
                <tr><td colspan="3"><i>You can find the bank holiday dates on this <a target="_blank" href="http://www.year-planner-calendar.wanadoo.co.uk/">website</a>.</i> </td></tr>
                <tr>
                    <th>Bank Holiday</th>
                    <th>Date</th>
                    <th>Notes</th>
                </tr>
                <% foreach (var bankHolidayExtended in Model.BankHolidays)
                   { %>
                    <% Html.RenderPartial("BankHolidaySummary", bankHolidayExtended); %>
                <% } %>
               <tr>
                    <td align="center" colspan="3" style="padding-top:20px;">
                        <input type="submit" value="Save"/>
                    </td>
                </tr>
                 <% if (ViewData["UpdatedFlag"] == "True")
                   { %>
                   <tr>
                        <td id="confirmationMessage" colspan="3">
                            At time <% Response.Write(DateTime.Now.ToString("T")); %> - Details have been successfully saved 
                        </td>
                   </tr>
                   <%}
                   else if (ViewData["UpdatedFlag"] == "False")
                   {%>
                   <tr>
                        <td id="Td1" colspan="3">
                            At time <% Response.Write(DateTime.Now.ToString("T")); %> - ERROR! Details have NOT been saved 
                        </td>
                   </tr>
                   <%} %>
            </table>
            </fieldset>
        <% } %>
        <script language="javascript" type="text/javascript">
            $(function () {
                $("#SelectedYear").change(function () {
                    var year = $("#SelectedYear").val();
                    $("#wholepage").load("/BankHoliday/Create/" + year);
                });
            });

    </script>
</asp:Content>

和控制器看起来像;

public ActionResult ListHistory(GridSortOptions sort,int?page,int?EmployeeStatusId)     {         if(Request.QueryString [“lastPersonMessage”] == null)             ViewData [“LastPersonMessage”] = string.Empty;         其他             ViewData [“LastPersonMessage”] = Request.QueryString [“lastPersonMessage”];

    IEnumerable<EmployeeExtended> employees = null;

    switch (EmployeeStatusId.GetValueOrDefault(1))
    {
        case 1: employees = EmployeeExtended.GetAllFormerEmployees();
            break;
        case 2: employees = EmployeeExtended.GetAllOnNoticeEmployees();
            break;
        case 3: employees = EmployeeExtended.GetAllCurrentEmployees();
            break;
    }
    if (sort.Column != null)
    {
        employees = employees.OrderBy(sort.Column, sort.Direction);
    }
    int pageLength = Convert.ToInt32(ConfigurationManager.AppSettings["EmployeeListPageLength"].ToString());
    employees = employees.AsPagination(page ?? 1, pageLength);
    ViewData["sort"] = sort;
    return View(employees);
}

1 个答案:

答案 0 :(得分:0)

你可以在Ajax.BeginForm()调用中进行调用,但是Url不会改变... - 意味着:每次刷新它时,它都会返回到默认页面..

我已经做了类似的事情:

<%: Html.DropDownList(Model => Model.SelectedYear, Model.YearList, new { onchange = "location.href='/Controller/Action/'+this.value" }) %>

这应该为解决问题提供一些基础。