I am sure this is a simple thing that I am missing. I was trying to add a dropdownlist to the Create.cshtml with the purpose of populating the list from a database. For the experiment I mucked up the following: two classes that correspond to database tables that have values.
namespace MvcTestApplication.Models
{
public class Complaints
{
public int ID { get; set; }
public string User { get; set; }
public DateTime ReleaseDate { get; set; }
public string Nature { get; set; }
public string Month { get; set; }
public string Year { get; set; }
public string Account { get; set; }
public string Employee { get; set; }
public string Manager { get; set; }
public int CompanyID { get; set; }
public int CompanyNumID { get; set; }
public int DepartmentID { get; set; }
public int ClientID { get; set; }
public int ClientCodeID { get; set; }
public string Source { get; set; }
public string Status { get; set; }
public virtual ICollection<Company> Companies{ get; set; }
}
namespace MvcTestApplication.Models
{
public class Company
{
public int CompanyID { get; set; }
public string CompanyName { get; set; }
public bool enabled { get; set; }
}
}
then a DAL
namespace MvcTestApplication.DAL
{
public class ComplaintDBContext : DbContext
{
public DbSet<Complaints> Complaints { get; set; }
}
On the create.cshtml page I just want to be able to populate a dropdownlist with the companies (many more dropdownlists after I get this working) but so far I always get a null reference whenever I try. On the create actionresult I tried passing a new Complaints model, just the complaints model, I am missing how to make the companies collection be populated.
<div class="editor-label">
@Html.LabelFor(model => model.ReleaseDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReleaseDate, new { id = "release_date" })
@Html.ValidationMessageFor(model => model.ReleaseDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Source)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Source)
@Html.ValidationMessageFor(model => model.Source)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CompanyID)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.CompanyID , new SelectList(Model.Companies , "CompanyID", "CompanyName"), "-- Select Company--")
@Html.ValidationMessageFor(model => model.CompanyID)
</div>