在发布更改之前用新关系更新对象

本文关键字:关系 更新 对象 新关系 布更改 | 更新日期: 2023-09-27 18:00:57

我有一个名为"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")
}

在发布更改之前用新关系更新对象

这将从数据库中获得一个不同字符串的列表。

控制器:

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中添加值,在名称中添加文本,从数据库结果中创建SelectListItems列表,可以更智能地处理此问题。这样以后更容易保存更改。