C# 如何在mvc linq EF中使用数据库结果填充下拉列表
这是我的控制器:C# 如何在mvc linq EF中使用数据库结果填充下拉列表,c#,asp.net,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc 3,这是我的控制器: public class MatchManagerController : Controller { // // GET: /MatchManager/ public ActionResult Index() { return View(); } public ActionResult CreateMatch() { return View(); } } 以下是我的看法: @
public class MatchManagerController : Controller
{
//
// GET: /MatchManager/
public ActionResult Index()
{
return View();
}
public ActionResult CreateMatch()
{
return View();
}
}
以下是我的看法:
@model MatchGaming.Models.MatchModel
@{
ViewBag.Title=CreateMatch;
}
创建匹配
@使用Html.begin{
@Html.ValidationSummarytrue
匹配模型
<div class="editor-label">
@Html.LabelFor(model => model.MatchName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MatchName)
@Html.ValidationMessageFor(model => model.MatchName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MatchDescription)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MatchDescription)
@Html.ValidationMessageFor(model => model.MatchDescription)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Wager)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Wager)
@Html.ValidationMessageFor(model => model.Wager)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MatchTypeId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MatchTypeId)
@Html.ValidationMessageFor(model => model.MatchTypeId)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
我希望我的MatchTypeId是从我的表MatchTypes填充的下拉列表。我假设数据库中有一个MatchTypes表,该表具有Id字段和描述字段。您希望显示一个显示不同MatchType描述的下拉列表 在模型中包含一个字段,该字段从MatchTypes表返回MatchTypes记录的集合。然后代替LabelFor执行以下操作:
@Html.DropDownListFor(m => m.Description, Model.MatchTypes)