I'm giving a go through some tutorials (here and here) on ASP.NET MVC, and decided to try a few things on my own. Now, I've got three tables, Resume, Descriptions, SubDescriptions. Here's the code for the three:
public class Resume
{
public Resume()
{
Descriptions = new List<Description>();
}
[Key]
public int ResumeId { get; set; }
[Required]
public string Employer { get; set; }
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
[DataType(DataType.Date)]
public DateTime EndDate { get; set; }
[Required]
public string Location { get; set; }
[Required]
public virtual ICollection<Description> Descriptions { get; set; }
}
public class Description
{
public Description()
{
SubDescriptions = new List<SubDescription>();
}
[Key]
public int DescriptionId { get; set; }
[ForeignKey("Resume")]
public int ResumeId { get; set; }
[Required]
public string Desc { get; set; }
public virtual Resume Resume { get; set; }
public virtual ICollection<SubDescription> SubDescriptions { get; set; }
}
public class SubDescription
{
[Key]
public int SubDescriptionId { get; set; }
[ForeignKey("Description")]
public int DescriptionId { get; set; }
[Required]
public string Sub { get; set; }
public virtual Description Description { get; set; }
}
Now, the problem I'm having is in the Create view. I'd like to create a new Resume entry. However, I'm running into an issue with adding Descriptions. Here's my Create view:
@model Portfolio.Models.Resume
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Resume</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Employer)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Employer)
@Html.ValidationMessageFor(model => model.Employer)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.StartDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.StartDate)
@Html.ValidationMessageFor(model => model.StartDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EndDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EndDate)
@Html.ValidationMessageFor(model => model.EndDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Location)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Location)
@Html.ValidationMessageFor(model => model.Location)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Descriptions)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Descriptions)
@Html.ValidationMessageFor(model => model.Descriptions)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
The problem is that since Descriptions is a list, nothing actually shows up to add. I'm not even sure about the way to go about this. I want to be able to add a Description, and then have a choice of adding a SubDescription after the fact. Is this even possible, or am I attempting something utterly convoluted?
EDIT
Adding this edit for a little more clarification. I'm trying to determine how to have the user populate the Descriptions list in the Create view. I was thinking of using a listbox for this. Then, they can select a Description, and add a SubDescription. But I'm not 100% sure on how to do that. I tried to change the @Html.EditorFor(model => model.Descriptions) to @Html.ListBoxFor(model => model.Descriptions), but I got an error that no overload for ListBoxFor takes 1 argument. And since I'm still new and learning this, I'm not sure how to go about accomplishing what I'd like to do.