繁体   English   中英

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

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

我有两个模型,一个 model(注册)有另一个 model(事件)一个外键。 我已经毫无问题地创建了多个事件对象,但是当我尝试创建注册 object 时,事件下拉字段为空。

在此处输入图像描述

我有以下代码:

模型/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”添加了一个新的 controller,这给了我这个:

注册控制器.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" })`

我发现了我的错误。

在创建 controller 时,使用“MVC 5 Controller with view, using Entity Framework”选项。 我应该为两者选择相同的“数据上下文类”。 我的错误是我使用 RegistrationDBContext 进行注册并使用 EventDBContext 进行事件。 但它应该都是RegistrationDBContext。

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

我现在的问题是我无法删除 EventDBContext。 但我只是通过创建一个新项目来修复它。 在我的新项目中,我没有创建任何 DBContext,只是使用了 Add Controller 对话框上的 + 图标。 (如果有错误只是重建你的解决方案)

暂无
暂无

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

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