c# - ASP.NET MVC 和 C# 中外键的空下拉列表
问题描述
我有两个模型,一个模型(注册)有另一个模型(事件)一个外键。我已经毫无问题地创建了多个事件对象,但是当我尝试创建注册对象时,事件下拉字段为空。
我有以下代码:
模型/Registration.cs
public class Registration
{
public int RegistrationId { get; set; }
// Foreign key
public int EventId { get; set; }
// Navigation properties
public virtual Event Event { get; set; }
}
public class RegistrationDBContext : DbContext
{
public DbSet<Registration> Registrations { get; set; }
public System.Data.Entity.DbSet<Assignment.Models.Event> Events { get; set; }
}
模型/事件.cs
public class Event
{
public int EventId { get; set; }
public string EventName { get; set; }
public string EventLocation { get; set; }
public DateTime EventDate { get; set; }
public virtual ICollection<Registration> Registrations { get; set; }
}
public class EventDBContext : DbContext
{
public DbSet<Event> Events { get; set; }
}
然后我使用选项“MVC 5 Controller with view, using Entity Framework”添加了一个新控制器,它给了我这个:
注册控制器.cs
// GET: Registrations/Create
public ActionResult Create()
{
ViewBag.EventId = new SelectList(db.Events, "EventId", "EventName");
ViewBag.ParticipantId = new SelectList(db.Participants, "ParticipantId", "Name");
return View();
}
视图/注册/Create.cshtml
@model Assignment2.Models.Registration
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Registration</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.RegistrationDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RegistrationDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RegistrationDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EventId, "EventId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("EventId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.EventId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ParticipantId, "ParticipantId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("ParticipantId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ParticipantId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
解决方案
您已将 SelectLists 放在 ViewBag 中的下拉列表中,并且您没有在 @Html.Dropdownlist 中使用它们
应该:
@Html.DropDownList("EventId", ViewBag.YouSelectList as SelectList, new { @class = "form-control" })
或者
@Html.DropDownListFor(model => model.EventId, ViewBag.SelectListServiceItems as SelectList, new { @class = "form-control" })`
推荐阅读
- sql - 导出 SQL Server 数据库时没有 PK 或 FK
- sqlite - 无法在 Sqllite 中指定选择列,但我看看我是否选择了所有列?
- mongodb - 如何更改子文档数组中的字段类型
- vba - 如何通过 VBA 设置 Outlook 电子邮件表格视图中的列顺序?
- racket - 创建结构实例时如何使用关键字参数
- jenkins - 这个詹金斯 groovy 脚本是如何工作的?
- javascript - 如何将 JQuery Datepicker 月份和年份显示更改为下拉菜单?
- haskell - Haskell:“没有由文字 '0.0' 引起的 (Fractional (Maybe Float)) 实例”
- javascript - discord.js 中等待消息反应的未定义错误
- groovy - 调试 groovy 脚本时使用类路径