首页 > 解决方案 > 如何将数据库表中的数据添加到列表中并在“CategoryPicker”中显示

问题描述

我有一个问题,我想将数据库表中的数据添加到列表中并将其显示在“CategoryPicker”中,现在我在 View 中创建了我的类别,如下所示:

div class="form-group">
        @Html.LabelFor(model => model.Category, "Type")
        @Html.DropDownListFor(m => m.Category, new List<SelectListItem>
        {
        new SelectListItem { Text="Funny", Value="Funny" },
        new SelectListItem { Text="Serious", Value="Serious" },
        new SelectListItem { Text="Stupid", Value="Stupid" }
        }, "Give a category of meme", new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" })
    </div>

我想看起来像这样: https : //imgur.com/aXxMZ5b 我创建模型 Memy 和 Category

 public partial class Memy
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [HiddenInput]
        public int Id_mema { get; set; }
        public string Autor { get; set; }
        [Required]
        public string Title { get; set; }
        [HiddenInput]
        public string coverImg { get; set; }

        public string Description { get; set; }
        [Required]
        public string Category { get; set; }
        [HiddenInput]
        public DateTime? releaseDate { get; set; }
        public DateTime? modifyDate { get; set; }
        public int? Like { get; set; }
        public int? Dislike { get; set; }
    }
public class Categories
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int IdCategory { get; set; }
        [Required]
        public string NameCategory { get; set; }
    }

标签: c#asp.net

解决方案


我像你写的那样

@model MemeGenerator.Models.Memy
@using <insert memy controller namespace here>
@{
    ViewData["Title"] = "CreateMeme";
}
<h2>CreateMeme</h2>
@using (Html.BeginForm("Create", "Memy", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="form-group">
        @Html.LabelFor(model => model.Title, "Title")
        @Html.TextBoxFor(model => model.Title, new { @class = "form-control", placeholder = "Give title" })
        @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.Description, "Description")
        @Html.TextBoxFor(model => model.Description, new { @class = "form-control", placeholder = "Give description" })
        @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
    </div>



    <div class="form-group">
        @Html.DropDownListFor(m => m, MemyController.GetCategories(),
                   "Give a category of meme")
    </div>

在 MemyController 中

 public class MemyController : Controller
    {

        private readonly UserManager<IdentityUser> _userManager;

        //1 MB
        const long fileMaxSize = 1 * 1024 * 1024;

        memyContext db = new memyContext();
        public class helpers
        {
            private readonly memyContext _context;

            public helpers(memyContext context)
            {
                _context = context;
            }

            public  IEnumerable<SelectListItem> GetCategories()
            {
                // your context

                return new SelectList(_context.Categories.OrderBy(x => x.NameCategory), "IdCategory ", "NameCategory ");
            }
        }

我有这个错误:当前上下文中不存在名称“GetCategories”。有人有这个问题吗?


推荐阅读