gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:44:18 24 4
gpt4 key购买 nike

  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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com