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/