1

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>
4
  • I am doing code first for the tables and I have verified that there is data there. The index page lists the sample complaints I seeded the table with correctly. Commented Jun 20, 2014 at 16:42
  • I guess I should add that this would be good to have in Edit mode as well (the dropdowns) Commented Jun 20, 2014 at 16:50
  • I think I got it to work: public ActionResult Create() { List<Company> comps = db.Companies.ToList(); return View(new Complaints { Nature = "test this", Source = "Email", ReleaseDate = DateTime.Now, Companies = comps }); //return View(); } Commented Jun 20, 2014 at 18:48
  • and now I am trying to work on how to display the value of the companyid (companyname) in the index view. Commented Jun 20, 2014 at 20:26

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.