2
  public enum Employee
    {
        FT,
        PT,
    }

これはうまくいきません

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

            return View("MyView");
        }

例外の詳細: System.ArgumentException: パラメーター ディクショナリには、'SampleControllerEx.Controllers.HomeController' のメソッド 'System.Web.Mvc.ActionResult Index(SampleControllerEx.Controllers.Employee)' のパラメーター 's' の無効なエントリが含まれています。ディクショナリには「System.Int32」型の値が含まれていますが、パラメーターには「SampleControllerEx.Controllers.Employee」型の値が必要です。パラメータ名: パラメータ

しかし、以下の作品は、

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

            return View("MyView");
        }

オプションのパラメーター (4.0) がサポートしていないのに、'DefaultValue' がカスタム列挙型のみをサポートする理由を教えてください。

4

1 に答える 1

1

次の方法で実行できます。

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

            return View("MyView");
        }
于 2013-04-09T18:53:21.633 に答える