HTML从数据库linq MVC创建下拉列表

时间:2015-06-30 11:11:28

标签: c# html asp.net-mvc asp.net-mvc-4

在我的项目中,我遇到了一个问题 我有一个产品表和一个供应产品的供应商表,因此在创建产品时我需要从数据库中选择一个特定的供应商我到目前为止尝试的代码只选择一个可能不存在的供应商ID

<div class="editor-label">
    @Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Name)
    @Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.SupplierId)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.SupplierId)
    @Html.ValidationMessageFor(model => model.SupplierId)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.Category)
</div>
<div class="editor-field">
   @Html.EditorFor(model => model.Category)
   @Html.ValidationMessageFor(model => model.Category)
</div>

1 个答案:

答案 0 :(得分:0)

您可以使用DropDownListFor helper

<div class="editor-label">
    @Html.LabelFor(model => model.Category)
</div>
<div class="editor-field">
   @Html.DropDownListFor(model => model.Category)
   @Html.ValidationMessageFor(model => model.Category)
</div>

MSDN Article