c# - 保存 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:对象引用未设置为对象的实例。
解决方案
在您看来,将代码更改为:
<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)
如果要设置dataValueField
and ,也可以使用 Constructor dataTextField
。我不知道你是如何得到你的ProfileList
以及它包含什么,因此为什么我只使用了SelectList(IEnumerable items);
构造函数。
在这里进一步阅读
推荐阅读
- javascript - 为什么这段代码返回一个空数组?
- asynchronous - 在同步或异步调用 asp.net core blazor 方法之间进行选择
- wordpress - Nginx:主域上的 Nuxtjs 应用程序 + 子目录中的 WordPress
- c - 从字符串中提取文件路径
- kubernetes - NodePort 与 ClusterIP 与 Kubernetes 上的无头服务的性能注意事项
- regex - 提取字符串中文件扩展名 (.ps1) 之前的数字
- node.js - 角材料表排序在帖子中不起作用代码
- mysql - 此查询的正确解决方案是什么
- c++ - 为对象指针实现复制构造函数和运算符的正确方法是什么
- machine-learning - Google Colab:即使在运行时断开连接后,我们还能恢复所有数据吗?