c# - 如何将数据库表中的数据添加到列表中并在“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; }
}
解决方案
我像你写的那样
@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”。有人有这个问题吗?
推荐阅读
- akka - 如何将数据从队列传递给客户端?
- neo4j - 实体列表之间存在关系
- php - Codeigniter :比较两个日期并将其显示在带有列表的表格上
- node.js - 如何查找用户已加入的所有课程?猫鼬
- elastic-stack - ELK(Elastic Stack)服务器运行进程的可视化
- php - 如何将 jquery 数据相应地/单独地填充到卡片中?
- java - 在 JOGL 中遇到 SSBO 问题
- python - 如何获取图像内矩形内的像素值
- python - TypeError: 'str' object is not callable while trying to use python swampy
- swift - Nested UIStackView child in UIScrollView does not stretch to fill