首页 > 解决方案 > 保存 Html.DropDownList

问题描述

我怎样才能将这个选定的选项保存到我的数据库中,现在它正在保存所有数据,但 ProfileText 是我需要的......我想我需要添加一个 asp-for 但我不知道在哪里说实话,或者如果是不同的方式,请告诉我。这是我的看法:

@model HCCBPOHR.Data.Candidate
@*@model HCCBPOHR.DomainModel.CandidateModel*@
 @*@model HCCBPOHR.Services.CandidateService.PostBoth*@
@{
ViewData["Title"] = "CandidateCreate";
}
<h2>CandidateCreate</h2>
<h4>Candidate</h4>
<hr />
<div class="row">
<div class="col-md-4">
    <form method="post" enctype="multipart/form-data" asp-action="CandidateCreate">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="Name" class="control-label"></label>
            <input asp-for="Name" class="form-control" />
            <span asp-validation-for="Name" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Number" class="control-label"></label>
            <input asp-for="Number" class="form-control" maxlength="9" />
            <span asp-validation-for="Number" class="text-danger"></span>
        </div>
        <div class="form-group">
            @Html.DropDownList("ProfileText", "Select Profile")
        </div>

        <div class="form-group">
            <label asp-for="CV" type="file" class="control-label"></label>
            <input asp-for="CV" type="file" class="form-control" />
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-default" onclick="this.disabled=true;this.form.submit();" />
        </div>
    </form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>

这是我的模型:

public class Candidate : BaseEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Number { get; set; }
    public string ProfileText { get; set; }
    public Byte[] CV { get; set; }
    public string CVNAME { get; set; }
    public List<Profile> ProfileList { get; set; }
}

这就是我将列表发送到视图的方式:

public IActionResult CandidateCreate(int postId)
    {

        using (var applicationcontext = new ApplicationContext())
        {
            IEnumerable<SelectListItem> items = applicationcontext.Profile.Select(c => new SelectListItem{Value = c.ProfileText,Text = c.ProfileText});
            ViewBag.ProfileText = items.ToList();
            return View();
        }
    }

我现在遇到的错误是

NullReferenceException:对象引用未设置为对象的实例。

标签: c#asp.netasp.net-core

解决方案


在您看来,将代码更改为:

<div class="form-group">
   <select asp-for="ProfileText" asp-items="@Model.ProfileList"></select>
</div>

然后在您的模型中,您将拥有一个我们可以调用的属性,ProfileText然后在提交表单时将其发布到服务器。

通过引入一个新的道具来改变你的模型,如下所示:

public SelectList ProfileList { get; set; }

现在在你的行动中,你需要做:

var model = new Candidate();
...
model.ProfileList = new SelectList(YourProfileListFromDbOrSomeWhereElse);
return View(model);

请注意,SelectList(IEnumerable, string dataValueField, string dataTextField)如果要设置dataValueFieldand ,也可以使用 Constructor dataTextField。我不知道你是如何得到你的ProfileList以及它包含什么,因此为什么我只使用了SelectList(IEnumerable items);构造函数。

在这里进一步阅读


推荐阅读