As posted by jfar, use:
string selectedValue = "1";
SelectListItem[] selectListItems = Enum.GetNames(typeof(MyEnumeration)).Select(
s => new SelectListItem { Text = s, Value = s, Selected = s == selectedValue}).ToArray();
which of MVCContrib, you should not include a DLL, this is just the code found in MVCContrib.
To protect against CSRF (Cross-Site Request Subroutine), you can use <%= Html.AntiForgeryToken() %>the presentation in the appropriate form to be placed and decorate the corresponding action with [ValidateAntiForgeryToken]. More information about Html.AntiForgeryToken()can be found here .
EDIT According to comment
Ok, first you need to put SelectListItem[]in ViewData so that you can access it in the view:
Act
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult MyView(string enumValue)
{
string selectedValue = "1";
SelectListItem[] selectListItems = Enum.GetNames(typeof(MyEnumeration)).Select(
s => new SelectListItem { Text = s, Value = s, Selected = s == selectedValue}).ToArray();
ViewData["enumValue"] = selectListItems;
return View();
}
and, in your opinion, the following form will work.
<form method="post">
<%= Html.AntiForgeryToken() %>
<%= Html.DropDownList("enumValue") %>
</form>
HTML- select.
, ,
[ValidateAntiForgeryToken]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult MyView(int enumValue)
{
ViewData["Message"] = "You selected the Enum name" + Enum.GetName(typeof(MyEnumeration), enumValue);
return View();
}