如何将数据库表中的数据添加到列表并在“ CategoryPicker”中显示

时间:2019-06-03 10:51:52

标签: c# asp.net

我遇到问题,我想将数据库表中的数据添加到列表中并在“ 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; }
    }

2 个答案:

答案 0 :(得分:0)

创建一种从数据库中获取列表的方法

public class helpers
{
    public static IEnumerable<SelectListItem> GetCategories()
    {
        // your context
        return new SelectList(myContext.Categories.OrderBy(x => x.NameCategory ), 
                                                      "IdCategory ", "NameCategory ");
    }
}

然后使用类似以下内容的链接:

@Html.DropDownListFor(m => m.Category, helpers.GetCategories(),
                  "Give a category of meme")

答案 1 :(得分:0)

我就像你写的

@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”在当前上下文中不存在。有人有这个问题吗?