简体   繁体   中英

HTML Creating a drop down list from database linq MVC

During my project I have encountered a problem I have a table of products and a table of suppliers supplying the products therefore when creating a product i need to choose a specific supplier from the data base the code I have tried so far just chooses an id of supplier which may not exist

<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>

You can use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM