簡體   English   中英

<select>來自asp.net數據庫的選項

[英]<select> options from database in asp.net

我有一個“類別”表所在的數據庫。 當我在管理面板中創建新聞時,我必須選擇一個類別。 我該如何實施?

編輯指南.cs

@model Guide
<form asp-controller="Panel" asp-action="EditGuides" method="post">
        <input asp-for="Id" type="hidden" />
        <div>
            <label>Title</label>
            <input asp-for="Title" />
        </div>
        <div>
            <label>Category</label>
            <select asp-for="???" ???></select>
        </div>
        <input type="submit" value="Submit" />
</form>

模型/類別.cs

namespace Project.Models
{
    public class Category
    {
        public int Id { get; set; }
        public string Title { get; set; } = "";
    }
}

asp-for 是為了您需要的值。 asp-items 用於您的類別列表項(從數據庫中獲取類別)。

@model Guide
@inject Project.Models.Category Category
<form asp-controller="Panel" asp-action="EditGuides" method="post">
        <input asp-for="Id" type="hidden" />
        <div>
            <label>Title</label>
            <input asp-for="Title" />
        </div>`enter code here`
        <div>
            <label>Category</label>
            <select asp-for="@Category.Id" asp-items="CatagoryList"></select>
        </div>
        <input type="submit" value="Submit" />
</form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM