Yes, it is possible. You can submit either the integer value or the string name of the enum value. A quick experiment verifies this. For example, in HomeController.cs write this.
public class HomeController : Controller {
public ActionResult Index() {
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(MyEnum value) {
ViewData["Message"] = value.ToString();
return View();
}
}
public enum MyEnum {
Foo,
Bar,
Baz
}
Haacked
Contributor
6901 Points
412 Posts
Re: enum args in controller action methods?
Jun 26, 2009 04:22 PM|LINK
Yes, it is possible. You can submit either the integer value or the string name of the enum value. A quick experiment verifies this. For example, in HomeController.cs write this.
public class HomeController : Controller { public ActionResult Index() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(MyEnum value) { ViewData["Message"] = value.ToString(); return View(); } } public enum MyEnum { Foo, Bar, Baz }Then in Index.aspx include the form:
<% using (Html.BeginForm()) { %> <%= Html.TextBox("value") %> <input type="submit" /> <% } %>And try submitting the value 0 then try submitting Bar and you'll see they both work.
Phil
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?