c# - 自定义枚举作为可选参数

标签 c# asp.net .net asp.net-mvc

  public enum Employee
    {
        FT,
        PT,
    }

这行不通

  public ActionResult Index(Employee s = Employee.PT)
        {
            ViewData["Message"] = s.ToString();

            return View("MyView");
        }

Exception Details: System.ArgumentException: The parameters dictionary contains an invalid entry for parameter 's' for method 'System.Web.Mvc.ActionResult Index(SampleControllerEx.Controllers.Employee)' in 'SampleControllerEx.Controllers.HomeController'. The dictionary contains a value of type 'System.Int32', but the parameter requires a value of type 'SampleControllerEx.Controllers.Employee'. Parameter name: parameters

但下面一个是可行的,

public ActionResult Index([DefaultValue(Employee.PT)] Employee s)
        {
            ViewData["Message"] = s.ToString();

            return View("MyView");
        }

我可以知道为什么 'DefaultValue' 只支持自定义枚举,而可选参数 (4.0) 不支持吗?

最佳答案

你可以这样做:

 public ActionResult Index(int employeeType)
        {
            Employee s = (Employee) employeeType;
            ViewData["Message"] = s.ToString();

            return View("MyView");
        }

关于c# - 自定义枚举作为可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15894681/

相关文章:

c# - Report Viewer .rdlc 如何传递参数?

c# - 实时观察 WPF 中写入的 CSV

.net - Azure 上的 "You do not have permission to view this directory or page"Mvc 4 应用程序

c# - 通过分隔符拆分字符串,将字符串列表转换为对象列表

c# - 如何直接在 C# 中创建以太坊钱包

c# - 为什么当 Alpha==0 时 System.Drawing.Graphics 会消隐 RGB channel ?

java - 从 JSP 调用后端 Java 类

asp.net - 如何允许匿名用户访问虚拟目录

c# - Visual Studio 不访问类 asp.net 的实例

.net - 识字编程