Asp.net mvc 4 如何在Razor页面中单击时更改ActionLink中的参数值

Asp.net mvc 4 如何在Razor页面中单击时更改ActionLink中的参数值,asp.net-mvc-4,razor,Asp.net Mvc 4,Razor,我有以下表格标题,它实际上是一个ActionLink: <th> @Html.ActionLink("Country Name", "Index", new { sortOrder = "CountryName", CurrentSort = ViewBag.CurrentSort }) </th> ViewBag只会在不同的操作之间切换,之后它将不可用,因此如果在索引后调用其他操作,则会丢失作为排序器发送的最后一个值 解决方案可以是使用TempData,它也可

我有以下表格标题,它实际上是一个ActionLink:

<th>
    @Html.ActionLink("Country Name", "Index", new { sortOrder = "CountryName", CurrentSort = ViewBag.CurrentSort })
</th>

ViewBag
只会在不同的操作之间切换,之后它将不可用,因此如果在索引后调用其他操作,则会丢失作为排序器发送的最后一个值

解决方案可以是使用
TempData
,它也可以在操作和多个请求之间持久化:

将操作代码更改为使用
TempData

  TempData["CurrentSort"] = sortOrder;

  sortOrder = String.IsNullOrEmpty(sortOrder) ? "CountryName" : sortOrder;
现在在视图中,使用
TempData.Peek()
读取值,以便将值保存在
TempData
中,以便再次读取:

 @Html.ActionLink("Country Name", 
                  "Index", 
                  new 
                  { 
                     sortOrder = "CountryName", 
                     CurrentSort = TempData.Peek("CurrentSort") as String 
                  }
                 )
完成以下操作:

 public ActionResult Index(string sortOrder, string CurrentSort, int? page)
 {

        int pageSize = 10;
        int pageIndex = 1;
        pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;

        sortOrder = String.IsNullOrEmpty(CurrentSort) ? "CountryName" : CurrentSort; 
        // toggling sort

        TempData["CurrentSort"] = sortOrder; 
        // persisting selected Sort

        IPagedList<Country> countries = null;

        if (sortOrder.Equals(CurrentSort))
        {
            countries = db.Countries.OrderByDescending(c => c.CountryName).ToPagedList(pageIndex, pageSize);
        }
        else
        {
            countries = db.Countries.OrderBy(c => c.CountryName).ToPagedList(pageIndex, pageSize);
        }
        return View(countries);
 } 
public ActionResult索引(字符串排序器、字符串当前排序、int?页)
{
int pageSize=10;
int pageIndex=1;
pageIndex=page.HasValue?将.ToInt32转换为(第页):1;
sortOrder=String.IsNullOrEmpty(CurrentSort)?“CountryName”:CurrentSort;
//切换排序
TempData[“CurrentSort”]=排序器;
//持久化所选排序
IPagedList国家/地区=空;
if(sortOrder.Equals(CurrentSort))
{
countries=db.countries.OrderByDescending(c=>c.CountryName).ToPagedList(pageIndex,pageSize);
}
其他的
{
countries=db.countries.OrderBy(c=>c.CountryName).ToPagedList(pageIndex,pageSize);
}
返回视图(国家);
} 

您可以阅读有关TempData的内容,也可以阅读关于上述代码是否对您不起作用?@EhsanSajjad否。ActionLink不起切换作用。它只会更改一次。您必须使用
TempData
,因为
ViewBag
值只会从一个动作持续到另一个动作view@EhsanSajjad您是否可以提供有关此问题的详细答案?基本上,我没有广泛使用ASP.NET MVC和Razor,上一次使用ASP.NET MVC几乎是1.5年前的事了。所以我忘了很多事情。我得到了编译错误<代码>CS0103:名称“CurrentSort”在当前上下文中不存在@Capt.JackSparrow抱歉,我的打字错误是TempData键,因此我们需要在双引号中加上,如果您在智能上看到,它将告诉您,
Peek()
将字符串作为参数是的,我找到并修复了它。但我仍然无法让它在“CountryName”和“”之间切换。它的行为是相同的。@Capt.JackSparrow您在操作中使用了错误的变量,您需要检查
CurrentSortOrder
在操作中不
sortorder
让我编辑我的postVai,它仍然没有进行切换。
 public ActionResult Index(string sortOrder, string CurrentSort, int? page)
 {

        int pageSize = 10;
        int pageIndex = 1;
        pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;

        sortOrder = String.IsNullOrEmpty(CurrentSort) ? "CountryName" : CurrentSort; 
        // toggling sort

        TempData["CurrentSort"] = sortOrder; 
        // persisting selected Sort

        IPagedList<Country> countries = null;

        if (sortOrder.Equals(CurrentSort))
        {
            countries = db.Countries.OrderByDescending(c => c.CountryName).ToPagedList(pageIndex, pageSize);
        }
        else
        {
            countries = db.Countries.OrderBy(c => c.CountryName).ToPagedList(pageIndex, pageSize);
        }
        return View(countries);
 }