I've done it as a member property of an object that is getting model bound, so the DMB can definitely do it that way (in that case, I represented the enum as a dropdownlist). NOt sure if it would work on its own. going from JS might be more complex; I'll
have to think about it.
Help those who have helped you... remember to "Mark as Answered"
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
}
skmcfadden
Member
36 Points
99 Posts
enum args in controller action methods?
Jun 26, 2009 03:08 PM|LINK
Is it possible use an enum argument in a controller action method? How would javascript represent an enum arg value when calling a controller action?
thanks!
enum arg action
paul.vencill
Contributor
6716 Points
1358 Posts
Re: enum args in controller action methods?
Jun 26, 2009 03:41 PM|LINK
I've done it as a member property of an object that is getting model bound, so the DMB can definitely do it that way (in that case, I represented the enum as a dropdownlist). NOt sure if it would work on its own. going from JS might be more complex; I'll have to think about it.
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?