Reputation: 11
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>
Upvotes: 1
Views: 1667
Reputation: 7691
I think you need to build a template for your complex object type Complaints. Something like this should get you started to define a custom editor template for Complaints. This will automatically be rendered for each element in the Complaints collection.
You need to store it in ~/Views/Shared/EditorTemplates/Complaints.cshtml:
@model Complaints
<div>
@Html.LabelFor(x => x.id):
@Html.EditorFor(x => x.id)
@Html.LabelFor(x => x.Nature):
@Html.EditorFor(x => x.Nature)
@Html.LabelFor(x => x.ReleaseDate):
@Html.EditorFor(x => x.ReleaseDate)
@Html.LabelFor(x => x.Source):
@Html.EditorFor(x => x.Source)
@Html.LabelFor(x => x.Status):
@Html.EditorFor(x => x.Status)
</div>
To us, simply write:
@Html.EditorFor(model => model.Complaints)
Upvotes: 1