Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 保存Html.DropDownList_C#_Asp.net_Asp.net Core - Fatal编程技术网

C# 保存Html.DropDownList

C# 保存Html.DropDownList,c#,asp.net,asp.net-core,C#,Asp.net,Asp.net Core,如何将选中的选项保存到数据库中,现在它正在保存所有数据,但ProfileText是我需要的。。。 我想我需要添加一个asp的,但我不知道在哪里诚实,或者如果它是一个不同的方式,请告诉我。 以下是我的看法: @model HCCBPOHR.Data.Candidate @*@model HCCBPOHR.DomainModel.CandidateModel*@ @*@model HCCBPOHR.Services.CandidateService.PostBoth*@ @{ ViewData["

如何将选中的选项保存到数据库中,现在它正在保存所有数据,但ProfileText是我需要的。。。 我想我需要添加一个asp的,但我不知道在哪里诚实,或者如果它是一个不同的方式,请告诉我。 以下是我的看法:

@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>
现在,在您的行动中,您需要做到:

var model = new Candidate();
...
model.ProfileList = new SelectList(YourProfileListFromDbOrSomeWhereElse);
return View(model);
请注意,如果要设置dataValueField和dataTextField,还可以使用SelectListIEnumerable、string dataValueField、string dataTextField构造函数。我不知道你是如何得到你的档案列表的,它包含了什么,所以为什么我只使用SelectListIEnumerable项;构造器


进一步阅读

如我所见,您没有填充dropdownlist。我认为更好的做法是在SelectList或string列表中选择值,并在模型中使用DropDownList for变量保存所选值。语法是:

@Html.DropDownListFormodel=>model.ProfileText,新建SelectListModel.ProfileList,name,新建{here Htmlattributes@class=表单控件}


完成此操作后,您将在发布模型时获得所选值

我遇到此错误:无法将类型“System.Collections.Generic.Kust”隐式转换为System.Collections.gEERIC.Inumerable确定我会将其添加到question@DorinMunteanu请在您填写公共列表的位置填写您的操作代码ProfileList@DorinMunteanu更新我的回答是,如果我的模型中的SelectList出现错误,请告诉我您是如何获得的。此行public SelectList ProfileList{get;set;}我收到一个错误:NullReferenceException:对象引用未设置为对象的实例您的视图希望接收一个模型,但您正在返回视图而不传递任何模型。在最后一行,您有返回视图{在这里传递您的模型}。并在模型中传递列表。不在可视包中。
var model = new Candidate();
...
model.ProfileList = new SelectList(YourProfileListFromDbOrSomeWhereElse);
return View(model);