我有一个名为" Professional"具有属性的名为" Skill"。
的其他对象的列表在我的编辑页面,我尝试做一些能够列出数据库中所有技能的内容,用户会选择某人并创建一个具有该技能的链接。
但我不知道如何实现这个目标:
我的编辑视图:
@model TCCApplication.Models.ProfessionalModel
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>ProfessionalModel</legend>
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.UserAccount.Id)
@Html.HiddenFor(model => model.UserAddress.Id)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.BirthDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.BirthDate)
@Html.ValidationMessageFor(model => model.BirthDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PhoneNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PhoneNumber)
@Html.ValidationMessageFor(model => model.PhoneNumber)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Profession)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Profession)
@Html.ValidationMessageFor(model => model.Profession)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserAccount.Username)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserAccount.Username)
@Html.ValidationMessageFor(model => model.UserAccount.Username)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserAccount.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserAccount.Password)
@Html.ValidationMessageFor(model => model.UserAccount.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserAddress.Street)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserAddress.Street)
@Html.ValidationMessageFor(model => model.UserAddress.Street)
</div>
<fieldset class="row">
<legend>Habilidades</legend>
@foreach (var skill in Model.Skills)
{
@Html.HiddenFor(sk => skill.Id)
<div class="col-md-10">
<div class="display-label">
@Html.Label("Nome da habilidade: ")
@Html.DisplayFor(sk => skill.Name)
</div>
<div class="display-label">
@Html.Label("Descrição")
@Html.DisplayFor(sk => skill.Description)
</div>
<div class="display-label">
@Html.Label("Categoria: ")
@Html.DisplayFor(sk => skill.Category.Name)
</div>
</div>
<div class="col-md-2">
@Html.ActionLink("Deletar Skill", "DeleteSkill", new { skillId = skill.Id, professionalId = Model.Id })
</div>
}
<p>
<div>
@Html.DropDownList("Skills", (IEnumerable<SelectListItem>)ViewBag.skills)
</div>
</p>
<div>
@Html.ActionLink("Adicionar habilidade não presente na lista", "AddNewSkill", new { professionalId = Model.Id})
</div>
</fieldset>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
答案 0 :(得分:0)
这将从数据库中获取不同字符串的列表。
控制器:
ViewBag.Skills = db.Skills.OrderBy(c=>c.Name).Select(c => c.Name).Distinct().ToList();
这会将先前收集的字符串放入多选列表中。
查看:
@Html.DropDownListFor(model => model.Skills, ((List<int>)ViewBag.Skills).Select(m=> new SelectListItem{Value = m.ToString(CultureInfo.InvariantCulture), Text = m.ToString(CultureInfo.InvariantCulture)}), new {multiple = "multiple"})
通过将值放入ID并将文本放入名称,可以从db结果的结果中创建SelectListItem列表,从而更智能地处理此问题。这样可以让以后更轻松地保存更改。