简体   繁体   English

ASP.NET MVC 和 C# 中外键的空下拉列表

[英]Empty drop down list for foreign key in ASP.NET MVC and C#

I have two models with one model (Registration) having the other model (Events) a foreign key.我有两个模型,一个 model(注册)有另一个 model(事件)一个外键。 I've created multiple Events objects without a problem, but when I try to create a Registration object, the Events dropdown field is empty.我已经毫无问题地创建了多个事件对象,但是当我尝试创建注册 object 时,事件下拉字段为空。

在此处输入图像描述

I have the following code:我有以下代码:

Models/Registration.cs模型/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; }       
}

Models/Events.cs模型/事件.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; }
}

I just then add a new controller using the option "MVC 5 Controller with view, using Entity Framework" which gives me this:然后我使用选项“MVC 5 Controller with view, using Entity Framework”添加了一个新的 controller,这给了我这个:

RegistrationsController.cs注册控制器.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();
}

Views/Registrations/Create.cshtml视图/注册/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")
}

You have put SelectLists for dropdowns in ViewBag and you are not using them in @Html.Dropdownlist您已将 SelectLists 放在 ViewBag 中的下拉列表中,并且您没有在 @Html.Dropdownlist 中使用它们

should be:应该:

@Html.DropDownList("EventId", ViewBag.YouSelectList as SelectList, new { @class = "form-control" })

or或者

@Html.DropDownListFor(model => model.EventId, ViewBag.SelectListServiceItems as SelectList, new { @class = "form-control" })`

I found my mistake.我发现了我的错误。

In creating the controller, with the option "MVC 5 Controller with view, using Entity Framework".在创建 controller 时,使用“MVC 5 Controller with view, using Entity Framework”选项。 I should have selected the same "Data context class" for both.我应该为两者选择相同的“数据上下文类”。 My mistake was I used RegistrationDBContext for Registration and used EventDBContext for Event.我的错误是我使用 RegistrationDBContext 进行注册并使用 EventDBContext 进行事件。 But it should be both RegistrationDBContext.但它应该都是RegistrationDBContext。

在此处输入图像描述 在此处输入图像描述

My problem now is I cant delete the EventDBContext.我现在的问题是我无法删除 EventDBContext。 But I fixed it by just creating a new project.但我只是通过创建一个新项目来修复它。 In my new project I didnt make any DBContext and just used the + icon on the Add Controller dialogue box.在我的新项目中,我没有创建任何 DBContext,只是使用了 Add Controller 对话框上的 + 图标。 (if there are errors just rebuild your solution) (如果有错误只是重建你的解决方案)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM