2
  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'. The dictionary contains a value of type 'System.Int32', but the parameter requires a value of type '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?

3
  • Sounds like your routing is wrong. Commented Apr 9, 2013 at 6:26
  • @leppie DefaultValue works with the same routing. why can't .net 4.0 option works properly? Commented Apr 9, 2013 at 6:32
  • It would be easier to make the parameter nullable. If you would address the parameter you can use if ((s ?? Employee.PT) == Employee.PT) { // some code } Commented Apr 9, 2013 at 9:01

1 Answer 1

1

You can do it in such way:

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

            return View("MyView");
        }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.