[英]Custom enum as optional parameter
public enum Employee
{
FT,
PT,
}
This doesn't work 这不行
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'.
异常详细信息:System.ArgumentException:参数字典包含“ SampleControllerEx.Controllers.HomeController”中方法“ System.Web.Mvc.ActionResult Index(SampleControllerEx.Controllers.Employee)”的参数“ s”的无效条目。 The dictionary contains a value of type 'System.Int32', but the parameter requires a value of type 'SampleControllerEx.Controllers.Employee'.
字典包含类型为'System.Int32'的值,但是该参数需要类型为'SampleControllerEx.Controllers.Employee'的值。 Parameter name: parameters
参数名称:参数
But below one works, 但以下一项有效,
public ActionResult Index([DefaultValue(Employee.PT)] Employee s)
{
ViewData["Message"] = s.ToString();
return View("MyView");
}
May I know why 'DefaultValue' only supports custom enum, where optional parameter(4.0) doesn't support it? 我可以知道为什么“ DefaultValue”仅支持自定义枚举,而可选参数(4.0)不支持该枚举吗?
You can do it in such way: 您可以通过以下方式进行操作:
public ActionResult Index(int employeeType)
{
Employee s = (Employee) employeeType;
ViewData["Message"] = s.ToString();
return View("MyView");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.