从HTML.Actionlink调用PostMethod

时间:2012-08-21 20:09:56

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

嗨,我对MVC很新,面临一个问题。

  • 我有一个视图,列出了所有只有2列的国家
  • 我希望列标题可点击,并且应该点击它排序
  • 我已经展示了我的控制器&查看下面的代码,我希望当我点击列标题时,它应该点击用[HttpPost]
  • 装饰的索引操作方法
  • 当我点击标题链接页面时,只需刷新而不按下Action方法,我希望它能够点击

任何帮助都将不胜感激。

这是我的控制器代码

[HttpPost]     public ActionResult Index(string sortcolumn)     {         return View(SortedList(sortcolumn));     }

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>

1 个答案:

答案 0 :(得分:1)

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

而不是

[HttpPost] 
public ActionResult Index(string sortcolumn) 

有像

这样的东西
public ActionResult Sort (string sortColumn)

并将您的actionlink指向Sort。