Skip to content

Instantly share code, notes, and snippets.

@cpoDesign
Created February 17, 2016 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpoDesign/0cc0a3c0b0fe457e3807 to your computer and use it in GitHub Desktop.
Save cpoDesign/0cc0a3c0b0fe457e3807 to your computer and use it in GitHub Desktop.
Example of implementation of @Html.DropDownListFor
public class DemoController : Controller
{
public ActionResult Index()
{
var model = new DemoModel();
model.Items = new List<SelectListItem>()
{
new SelectListItem()
{
Text = "one",
Value = "1",
Selected = false
},
new SelectListItem()
{
Text = "two",
Value = "2",
Selected = true
}
};
return View(model);
}
}
public class DemoController : Controller
{
public ActionResult Index()
{
var model = new DemoModel();
model.Items = new List<SelectListItem>()
{
new SelectListItem()
{
Text = "one",
Value = "1"
},
new SelectListItem()
{
Text = "two",
Value = "2"
}
};
model.ItemValue = 2;
return View(model);
}
}
public class DemoModel
{
public IEnumerable<SelectListItem> Items { get; set; }
public int ItemValue { get; set; }
}
@model YourNameSpace.DemoModel
@Html.DropDownListFor(x=>x.ItemValue,Model.Items)
@model YourNameSpace.DemoModel
@Html.DropDownListFor(x=>x.ItemValue, new SelectList(Model.Items,"Value","Text"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment