【问题标题】:Calling PostMethod from HTML.Actionlink从 HTML.Action 调用 PostMethod 链接
【发布时间】:2012-08-21 20:09:56
【问题描述】:

您好,我对 MVC 还很陌生,遇到了一个问题。

  • 我有一个视图列出了所有只有 2 列的国家/地区
  • 我希望列标题可点击,并且应该在点击它时进行排序
  • 我在下面显示了我的控制器和视图代码,我希望当我单击列标题时,它应该会点击用 [HttpPost] 装饰的索引操作方法
  • 当我点击标题链接页面时,页面只是刷新而不点击 Action 方法,我希望它会点击

任何帮助将不胜感激。

这是我的控制器代码

[HttpPost] 公共 ActionResult 索引(字符串排序列) { 返回视图(排序列表(排序列)); }

private List<Country> SortedList(string sortcol)
{
    List<Country> sortedlist = new List<Country>();
    switch (sortcol)
    {
        case "idCountry":
            if ((SortOrder)ViewData["sortorder"] == SortOrder.Ascending)
            {
                sortedlist = db.Countries.OrderByDescending(c => c.idCountry).ToList<Country>();
                ViewData["sortorder"] = SortOrder.Descending;
            }
            else
            {
                sortedlist = db.Countries.OrderBy(c => c.idCountry).ToList<Country>();
                ViewData["sortorder"] = SortOrder.Ascending;
            }
            break;
        case "Countryname":
            if ((SortOrder)ViewData["sortorder"] == SortOrder.Ascending)
            {
                sortedlist = db.Countries.OrderByDescending(c => c.Countryname).ToList<Country>();
                ViewData["sortorder"] = SortOrder.Descending;
            }
            else
            {
                sortedlist = db.Countries.OrderBy(c => c.Countryname).ToList<Country>();
                ViewData["sortorder"] = SortOrder.Ascending;
            }
            break;
    }
    return sortedlist;
}

这是我的看法

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

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        List of Countries</h2>
    <p>
        Manage list of countries on this page. You can choose your action creating, editing,
        removing the country data.
    </p>
    <% using (Html.BeginForm("Index","Country")) { %>
    Total Countries : <%: Model.Count() %>
    <table>
        <tr>
            <th>
                <%: Html.ActionLink("CountryID","Index",new { sortcolumn = "CountryID" }) %>
            </th>
            <th>
                <%: Html.ActionLink("CountryName ", "Index", new { sortcolumn = "CountryName" })%>
            </th>
            <th>
            </th>
        </tr>
        <% if (Model != null)
           {  %>
        <% foreach (var item in Model)
       { %>
        <tr>
            <td>
                <%: item.idCountry%>
            </td>
            <td>
                <%: item.Countryname%>
            </td>
            <td>
                <%: Html.ActionLink("Edit", "Edit", new { id = item.idCountry })%>
                |
                <%: Html.ActionLink("Details", "Details", new { id = item.idCountry })%>
                |
                <%: Html.ActionLink("Delete", "Delete", new { id = item.idCountry })%>
            </td>
        </tr>
        <% }
           }%>
    </table>
    <%} %>
    <p>
        <%: Html.ActionLink("Create New", "Create") %>
    </p>
</asp:Content>

【问题讨论】:

    标签: asp.net-mvc sorting html.actionlink


    【解决方案1】:

    您不能使用 Action 链接点击 post 方法。如果您所做的只是在这里排序,您可以做的一件事是将您的 post 方法更改为 get 并更改名称

    代替

    [HttpPost] 
    public ActionResult Index(string sortcolumn) 
    

    有类似的东西

    public ActionResult Sort (string sortColumn)
    

    并将您的操作链接指向排序。

    【讨论】:

    • 非常感谢,我能够按照您的建议完成我的目标。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    相关资源
    最近更新 更多