如何将枚举值传递给@Html.ActionLink

5

我有一些方法

public ActionResult ExportToCSV(CellSeparators cellSeparator)
{
  //
}          

public enum CellSeparators
{
   Semicolon,
   Comma
}

我们应该如何在HTML中正确地引用那个方法?
@Html.ActionLink("Exportar al CSV", "ExportToCSV", new { ?? })

谢谢你!
2个回答

3
在您的View.cshtml文件中:
@Html.ActionLink("Exportar al CSV", "ExportToCSV", new { cellSeparator=CellSeparators.Semicolon })

在你的控制器中:

public ActionResult ExportToCSV(CellSeparators? cellSeparator)
{
  if(cellSeparator.HasValue)
  {
    CellSeparator separator = cellSeparator.Value;
  }

  /* ... */
}

2

@Html.ActionLink("导出CSV", "ExportToCSV", new { cellSeparator=(int)CellSeparators.Semicolon })

以及

public ActionResult ExportToCSV(int cellSeparator)
{
  CellSeparator separator = (CellSeparator)cellSeparator;
}

虽然不够优雅,但却非常有用


1
另外,RouteValueDictionary也可以用于将枚举集合传递给ASP.NET MVC ActionMethod。 - Junior Mayhé

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接