我正在浏览一些关于 ASP.NET MVC 的教程(此处和此处),并决定自己尝试一些事情。现在,我有三张桌子Resume,,,,。以下是三者的代码:DescriptionsSubDescriptions
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; }
}
现在,我遇到的问题是在Create视图中。我想创建一个新Resume条目。但是,我遇到了添加Descriptions. 这是我的Create看法:
@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")
}
问题是,由于Descriptions是一个列表,实际上没有任何内容可以添加。我什至不确定该怎么做。我希望能够添加 a Description,然后可以选择在SubDescription事后添加 a 。这甚至可能吗,还是我正在尝试一些完全令人费解的事情?
编辑
添加此编辑以进行更多说明。我正在尝试确定如何让用户在视图中填充Descriptions列表。Create我正在考虑listbox为此使用 a 。然后,他们可以选择一个Description,并添加一个SubDescription。但我不是 100% 确定如何做到这一点。我试图将 更改@Html.EditorFor(model => model.Descriptions)为@Html.ListBoxFor(model => model.Descriptions),但我收到了一个错误,no overload for ListBoxFor takes 1 argument.而且由于我还是新手并且正在学习这个,我不知道如何去完成我想做的事情。