【问题标题】:Why is my controller action not being called upon re-clicking the button?为什么在重新单击按钮时没有调用我的控制器操作?
【发布时间】:2011-02-20 10:55:57
【问题描述】:

首先,一些代码:

这是页面导航到时调用的初始操作:

public ActionResult ListCodeTypes()
{
    var CodeList = _entities.Master_Codes.ToList();

    List<SelectListItem> items = new List<SelectListItem>();
    for (int i = 0; i < CodeList.Count; i++)
    {
        items.Add(new SelectListItem { 
            Text = CodeList[i].description, 
            Value = CodeList[i].code_table_name 
        });
    }

    ViewData["items"] = items;
    return View("CodesAdmin");
}

这里是 CodesAdmin aspx 页面/查看代码:

<%@ Page Title="" Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master"
    Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

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

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% Html.RenderPartial("subMenu/ParcelSubMenu"); %>
    <h2>CodesAdmin</h2>
    <%= Html.DropDownList("items")%>
    <input type="button" id="btnGetCodes" name="GetCodes" 
        value="Get Codes" class="fg-button ui-state-default appButton"
        onclick="searchClick()"/>
    <div id="CodeAdminPlaceHolder"></div>
    <script type="text/javascript">
        function searchClick() {
            var searchText = $("#items").val();
            $.get("/Admin/CodeListing/", 
                { identifier: searchText }, function (data) {  
                    $("#CodeAdminPlaceHolder").html(data); 
                }, "html");
        }
    </script>
</asp:Content>

现在,这是从按钮单击调用的控制器代码:

public ActionResult CodeListing(string identifier)
{
    _entities.Refresh(System.Data.Objects.RefreshMode.StoreWins,
        _entities.Address_Type_Codes);

    var CodeList = _entities.Address_Type_Codes.
        Where(p=>p.active == true).ToList();

    return PartialView("ListOfAddrCodes", CodeList);
}

CodeListing 操作实际上根据“标识符”参数处理多个输入。

在“CodeAdminPlaceHolder”div 中呈现的部分视图是:

<div id="Div15"  class="ui-widget-content">
    <table id="CodeListing" class="tablesorter">
        <thead>
            <tr>
                <th></th>
                <th>
                    Short Description
                </th>
                <th>
                    Long Description
                </th>
                <th>
                    Active
                </th>
                <th>
                    Tax Year
                </th>
                <th>
                    Note
                </th>
            </tr>
        </thead>

        <tbody>
            <% foreach (var item in Model) { %>

                <tr>
                    <td>
                        <%= Html.ActionLink("Deactivate", 
                            "DeactivateAddrCode", new { 
                                id=item.address_type_codes_id 
                             }) %>
                    </td>
                    <td>
                        <%: item.short_description %>
                    </td>
                    <td>
                        <%: item.long_description %>
                    </td>
                    <td>
                        <%: item.active %>
                    </td>
                    <td>
                        <%: item.tax_year %>
                    </td>
                    <td>
                        <%: item.note %>
                    </td>
                </tr>

            <% } %>
        </tbody>

        <tfoot>
            <tr id="pager">
                <td colspan="6">
                    <img src="/Content/TableSorter/Themes/Default/first.png"
                        class="firstPage" alt="First" />
                    <img src="/Content/TableSorter/Themes/Default/prev.png" 
                        class="prevPage" alt="Prev"/>
                    <input type="text" class="pagedisplay"/>
                    <img src="/Content/TableSorter/Themes/Default/next.png" 
                        class="nextPage" alt="Next"/>
                    <img src="/Content/TableSorter/Themes/Default/last.png" 
                        class="lastPage" alt="Last"/>
                    <select class="pagesize">
                        <option selected="selected" value="5">5</option>
                        <option value="10">10</option>
                        <option value="15">15</option>
                        <option  value="20">20</option>
                    </select>
                </td>
            </tr>
        </tfoot>
    </table>
</div>

如果有人单击“停用”链接,则调用此操作:

public ActionResult DeactivateAddrCode(int id)
{
    Address_Type_Codes ac1 = _entities.Address_Type_Codes.
        First(c => c.address_type_codes_id == id);

    ac1.active = false;
    _entities.Refresh(System.Data.Objects.RefreshMode.ClientWins, 
        _entities.Address_Type_Codes);

    UpdateModel(ac1);

    _entities.SaveChanges();

    return RedirectToAction("ListCodeTypes");
}

这会将页面加载到带有下拉框和按钮的开头。

问题是,当我单击按钮加载地址类型时,它不会调用“CodeListing”控制器操作。我通过在“CodeListing”代码中放置一个断点来验证这一点,果然,代码没有被调用。因此,当在下拉列表中进行选择并单击按钮时,会显示无效数据。如果我在 VS 中停止并重新启动应用程序,数据会正确列出。

CodeListing 动作只被调用一次,我猜缓存的页面被返回了。

我们正在使用实体框架进行数据访问,但我认为我们的问题与此无关。

【问题讨论】:

  • 为了澄清,只要有人选择了尚未选择的选择并单击搜索按钮,就会调用 CodeListing 操作。但是,如果用户选择了之前选择的选项,页面似乎会刷新缓存信息,并且不会调用控制器操作。

标签: c# jquery model-view-controller partial-views partial


【解决方案1】:

CodeListing 方法不是必须用 [HttpPost] 属性修饰才能接收提交按钮创建的 POST 请求吗?我所有的“按钮响应方法”都有它,我似乎记得没有应用该属性将意味着该方法不会被调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-26
    • 2016-01-27
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多