首页 > 解决方案 > 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")
}

标签: c#asp.net-mvcentity-framework

解决方案


您已将 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" })`

推荐阅读