我有这个:
<div class="editor-field">
<%: Html.DropDownListFor(m => m.Language, new SelectList(new[] { "string1", "string2", "string3", "string4" }, "string1"))%>
</div>
我希望字符串有一些值。例如,string1,有一个值= 1,string2:value = 2,等等。如何做到这一点?
答案 0 :(得分:2)
只需使用
new List<SelectListItem>(){ new SelectListItem{Text="item1", Value = "valuer1", ... }
或使用SelectList但传入其中Dictionary或List&lt;&gt;自定义对象。
答案 1 :(得分:0)
静态绑定
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to the Training Courses...";
List objcourses = new List();
objcourses.Add("Asp.Net");
objcourses.Add("MVC");
objcourses.Add("WCF");
objcourses.Add("WPF");
objcourses.Add("C#.Net");
ViewBag.Courses = new SelectList(objcourses);
return View();
}
}
@{
ViewBag.Title = "Home Page";
}
Index
@using(@Html.BeginForm(“Index”,”Home”,FormMethod.Get)) {
Courses List; @Html.DropDownList(“Courses“)
}
动态绑定
public class HomeController : Controller
{
public ActionResult Index()
{
private MovieDBContext db = new MovieDBContext();
var GenreLst = new List();
var GenreQry = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreLst.AddRange(GenreQry.Distinct());
ViewBag.Courses = new SelectList(GenreLst);
return View();
}
}
@{
ViewBag.Title = "Home Page";
}
Index
@using(@Html.BeginForm("Index","Home",FormMethod.Get)) {
Courses List; @Html.DropDownList("Courses")
}