Trying to populate a dropdownlist field with data from a model. When running the code I am getting an error that reads: "Exception thrown: 'System.InvalidOperationException' in System.Web.Mvc.dll" in the VS2017 Output screen.
The Model:
namespace FulfillmentPortal.Models
{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Collections.Generic;
public partial class Carrier
{
public int CarrierId { get; set; }
[StringLength(50)]
public string CarrierName { get; set; }
}
public partial class CarrierModel : DbContext
{
public CarrierModel()
: base("name=CarrierModel")
{
}
public virtual DbSet<Carrier> Carriers { get; set; }
public virtual DbSet<CarrierService> CarrierServices { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
}
The Controller:
namespace FulfillmentPortal.Controllers
{
public class FulfillmentController : Controller
{
private CarrierModel db = new CarrierModel();
// GET: Fulfillment
public ActionResult Index()
{
var carrierList = db.Carriers.ToList();
return View(carrierList);
}
}
}
The View:
@model FulfillmentPortal.Models.Carrier
@{
ViewBag.Title = "index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="panel panel-primary">
<div class="panel-heading">REPORT OPTIONS</div>
<div class="panel-body">
<form id="processForm" class="form-horizontal" action="~/Fulfillment/Report" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="sel1">Carrier:</label>
@Html.DropDownListFor(m => m.Carriers, new SelectList(Model.Carriers, "CarrierId", "CarrierName"), " ");
<label for="sel2">Carrier Services:</label>
<select class="form-control" id="sel2" style="width: auto; margin-bottom:15px;">
<option value="-1" selected>Select One</option>
</select>
</div>
</form>
</div>
</div>
Any help would be appreciated.