0

嗨,我对 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>
4

1 回答 1

1

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

代替

[HttpPost] 
public ActionResult Index(string sortcolumn) 

有类似的东西

public ActionResult Sort (string sortColumn)

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

于 2012-08-21T20:32:51.297 回答