CSharp · Mvc
using System;
using System.Web.Mvc;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace DropDownModelExample
{
public class DropDownModel
{
public int ID3 { get; set; }
public int ID4 { get; set; }
public int ID5 { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
}
}
@model DropDownModelExample.DropDownModel
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Drop Down Exmpale</title>
</head>
<body>
<div>@Html.DropDownListFor(m => m.ID3, Model.Items)</div>
<div>@Html.DropDownListFor(m => m.ID4, Model.Items)</div>
<div>@Html.DropDownListFor(m => m.ID5, Model.Items)</div>
</body>
</html>
using System;
using System.Web.Mvc;
using System.Collections.Generic;
namespace DropDownModelExample
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
var model = new DropDownModel
{
ID3 = 3,
ID4 = 4,
ID5 = 5,
Items = new List<SelectListItem>
{
new SelectListItem { Text = "First (Default)", Value = "1" },
new SelectListItem { Text = "Second (Selected)", Value = "2", Selected = true },
new SelectListItem { Text = "Third", Value = "3" },
new SelectListItem { Text = "Forth", Value = "4" },
}
};
return View(model);
}
}
}