我正在浏览一些关于 ASP.NET MVC 的教程(here 和 here),并决定自己尝试一些东西。现在,我有三个表,Resume
、Descriptions
、SubDescriptions
。这是三者的代码:
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
View 中。我想创建一个新的 Resume
条目。但是,我在添加 Descriptions
时遇到了问题。这是我的 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")
}
问题在于,由于 Descriptions
是一个列表,实际上没有显示要添加的内容。我什至不确定如何解决这个问题。我希望能够添加一个 Description
,然后可以选择在事后添加一个 SubDescription
。这甚至有可能吗,还是我正在尝试一些完全令人费解的事情?
编辑
添加此编辑以进一步说明。我正在尝试确定如何让用户填充 Create
View 中的 Descriptions
列表。为此,我正在考虑使用 listbox
。然后,他们可以选择一个Description
,并添加一个SubDescription
。但我不是 100% 确定如何做到这一点。我试图将 @Html.EditorFor(model => model.Descriptions)
更改为 @Html.ListBoxFor(model => model.Descriptions)
,但出现错误ListBoxFor 没有重载需要 1 个参数。
而且由于我还是新手并且正在学习这个,所以我不确定如何去完成我想做的事情。
最佳答案
你走对了! ListBoxFor
方法有两个参数。首先是你目前拥有的。第二个参数是 IEnumerable<SelectListItem>
对象。
我以前亲自做过这类事情,使用 ListBoxFor
真的很有帮助方法。您可以创建一个 List<SelectListItem>
每个项目都可以保存存储在 Descriptions 对象中的信息。
如果你通过 List<SelectListItem>
从您的 Controller 将对象添加到 View ,您可以向用户显示所有描述对象,他们可以选择并添加他们选择的对象。
您还可以创建单独的 View ,并在特定情况下根据您尝试执行的操作将用户重定向到这些 View 。
祝你好运! =)
关于c# - 通过用户输入将项目添加到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19850859/