How to use System.Web.Mvc.SelectListItem

How to use System.Web.Mvc.SelectListItem for asp.net mpc controller.

+3
source share
1 answer

1. The specialized collection SelectItemList generates code in the Controller / Action method.

  ViewData["list"] = new List<SelectListItem>
                       {
                         new SelectListItem {Text = "January", Value = "1"},
                         new SelectListItem {Text = "February", Value = "2"},
                         new SelectListItem {Text = "March", Value = "3"}
                       };

2.Read the rendering code in aspx views.

<% using (Html.BeginForm()) { %>
  <% = Html.DropDownList("list") %>

  <input type="submit" value="send" />
<% } %>

3. Enter the value code in the Controller / Action method with ModelBinder.

[HttpPost]
public ActionResult Index(string list)
{
  // process code
}

How about this code? The best as regards.

+8
source

Source: https://habr.com/ru/post/1720441/


All Articles