12 December 2013

Create A Simple DropDownList in MVC

You can create a DropDownList in Controller using the following script. The purpose of this is to simply give you an idea how to easily create a DropDownList in MVC.

First, create a class like this:

public class DropDownList
{
  public int Id { get; set; }
  public string Name { get; set; }
  public IEnumerable <SelectListItem> Names { get; set; }
  public IEnumerable <SelectListItem> Ids { get; set; }
}

Next, create an ActionResult like this:

public ActionResult MyDropDownList (DropDownList ddl)
{
  ddl.Names = new SelectList(ctx.Names.ToArray(), .......



Yeah. I know it's not complete. I intended to leave it that way to make you think how you could make it even better. Again, the purpose is to show you how simple to create a DDL in your controller.

No comments:

Post a Comment