简体   繁体   中英

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. I've created multiple Events objects without a problem, but when I try to create a Registration object, the Events dropdown field is empty.

在此处输入图像描述

I have the following code:

Models/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

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:

RegistrationsController.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

@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

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". I should have selected the same "Data context class" for both. My mistake was I used RegistrationDBContext for Registration and used EventDBContext for Event. But it should be both RegistrationDBContext.

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

My problem now is I cant delete the 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. (if there are errors just rebuild your solution)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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