ASP.NET MVC Output

Can someone point me to an article in which the drop-down list is populated from linq to sql (text and value set).

Thanks Danny

+7
asp.net-mvc
source share
4 answers

Now that the HtmlHelper extension accepts IEnumerable<SelectListItem> , I do not create a SelectList, but usually just create a SelectListItems with LINQ.

controller

 ViewData["CategoryID"] = categories.Select( c => new SelectListItem { Text = c.CategoryName, Value = c.CategoryID } ); 

View

 <%= Html.DropDownList("CategoryID") %> 

or if I need a default choice

 <%= Html.DropDownList("CategoryID", (IEnumerable<SelectListItem>)ViewData["CategoryID"], "Select a Category" ) %> 

EDIT :

An interesting bit about the drop-down list is that you need to provide a series of values ​​from which you can select a single value that matches your actual data model. I usually provide a range (menu items) through the view data and expect the model values ​​to return when this page is published. If you need strictly typed menus, you need to provide a view-only model that will encapsulate your real model and any menus. This assumes, upon publication, the use of prefixes to identify model elements. The trade-off, in my opinion, is a simpler binding of the model to the message against the use of strongly typed menus in the view. I did not hang up on the latter, so I decided not to put my menus in the model. If you want to do this, it might look like this.

Model

 public class CategoryViewModel { public Category Category { get; set; } public IEnumerable<SelectListItem> CategoryMenu { get; set; } ... } 

controller

Display action

 var model = new CategoryViewModel(); model.CategoryMenu = categories.Select( c => new SelectListItem { Text = c.CategoryName, Value = c.CategoryID } ); ... return View(model); 

Create action

 [AcceptVerbs( HttpVerbs.Post )] public ActionResult Create( [Bind(Prefix="Category")]Category category ) { ... } 

View

 <%= Html.TextBox("Category.Name") %> <%= Html.DropDownList("Category.CategoryID", Model.CategoryMenu, "Select a Category" ) %> 
+9
source share

Here's one great article by Rob Connery

Controller code

 NorthwindDataContext db = new NorthwindDataContext(); var categories = from c in db.Categories select c; ViewData["CategoryID"] = new SelectList(categories, "CategoryID", "CategoryName"); 

View markup

 <%=Html.DropDownList("CategoryID")%> 
+5
source share

If you need to add html attributes to your tags, this will be the way to do it. Pass the model to your view ei "return View (someModel)", then in the view:

  <select id="Groups" name="Groups"> <% foreach (SelectListItem item in Model.GroupsDropDown) { if (item.Selected) {%> <option selected="selected" title="<%= item.Text %>"> <%= item.Text%></option> <%} else {%> <option title="<%= item.Text %>"> <%= item.Text%></option> <%} %> <% } %> </select> 

GroupsDropDown is a property in your model as follows:

public IEnumerable GroupsDropDown {get; set; }

0
source share

Write in the view:

 @{ TaskManagerContext context = new TaskManagerContext(); IEnumerable<TestTask1.Models.User> CDrop = context.Users.ToList(); List<SelectListItem> selectList = new List<SelectListItem>(); foreach (var c in CDrop) { SelectListItem i = new SelectListItem(); i.Text = c.Username.ToString(); i.Value = c.ID.ToString(); selectList.Add(i); } 

}

You can refer to it like this:

 @Html.DropDownListFor(m => m.UserID, new SelectList(selectList, "Value", "Text")); 

You can also select a specific line:

 TaskManagerContext context = new TaskManagerContext(); UsersRepository repo = new UsersRepository(); User user = repo.GetAll().FirstOrDefault(u => u.ID == ViewBag.UserId); ViewBag.User = user; <div><h3><label>@ViewBag.Title1</label>@ViewBag.User.Username</h3></div> 
0
source share

All Articles