【问题标题】:MVC drop down list that controls a grid - what is the best way to get this to work?控制网格的 MVC 下拉列表 - 让它工作的最佳方法是什么?
【发布时间】:2010-10-26 15:39:47
【问题描述】:

我在客户端有一个下拉列表。当用户进行选择时,我的 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);
}

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

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

    我已经做过类似的事情了:

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

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

    【讨论】:

    • 如果您要在控制器上调用的方法具有以下签名,您会怎么做; public ActionResult ListHistory(GridSortOptions sort, int? page, int? EmployeeStatusId)
    • 你可以尝试类似:new { onchange = "location.href='/Controller/Action?sort=SomeValue&amp;page=SomeValue&amp;EmployeeStatusId=SomeValue }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 2011-12-17
    • 1970-01-01
    • 2010-11-04
    • 2011-04-07
    • 2010-09-16
    • 1970-01-01
    相关资源
    最近更新 更多