【问题标题】:DropdownList value doesnt add to databaseDropdownList 值不添加到数据库
【发布时间】:2018-06-11 15:57:53
【问题描述】:

我可以在下拉列表和需要值的位置显示数据库中的值。 但是在创建某些东西时,我无法从下拉列表中获取值到我的数据库中。它变得空了。 我尝试了一些来自 s.o.f 的解决方案,但没有奏效。

模型 1:

    public class Kategori
{
    [Key]
    public int KategoriID { get; set; }
    public string Namn { get; set; }    
}

模型 2:

    public class Inlägg
{
    [Key]
    public int InläggsID { get; set; }     
    public Kategori Kategori { get; set; }
}

控制器:

        // POST: Inlägg/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("Titel,Text,Kategori.Namn")] Inlägg inlägg)
        //The Kategori is getting null
    {
        if (ModelState.IsValid)
        {
            inlägg.Datum = DateTime.Now;
            _context.Add(inlägg);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(inlägg);
    }

查看:

            @Html.DropDownList("Kategori", null, htmlAttributes: new { @class = "form-control" })

我尝试过使用 SelectItemList,使用选项值进行选择,在 Models 类中有一个 SelectItem,在 Inlägg 中有一个“公共类别列表”。 真的不知道如何解决这个问题。我今天刚试了8个小时,昨天试了2个小时。 如何获取用户在下拉列表中选择的值而不是获取 null?告诉我是否需要发送更多代码 :-)

【问题讨论】:

    标签: asp.net .net razor asp.net-core-mvc


    【解决方案1】:

    你应该改变它;

     @Html.DropDownList("Kategori", null, htmlAttributes: new { @class = "form-control" })
    

     @Html.DropDownList("SelectedCategory", ViewData["Kategori"] as SelectList, htmlAttributes: new { @class = "form-control" })
    

    选定的下拉元素作为SelectedCategory 传递到服务器端。 另外,我强烈建议您使用Model 类而不是ViewData 在控制器和视图之间传输数据。

    【讨论】:

    • 还是空
    【解决方案2】:

    您需要为外键值添加另一个属性。由于您的其他相关实体类名称是Kategori,您可以将此新属性命名为KategoriId,以便它与外键属性名称的约定相匹配。

    public class Inlagg
    {
        [Key]
        public int InläggsID { get; set; }
        public string Titel { get; set; }
        public string Text { get; set; }
    
        public DateTime Datum { get; set; }   
    
        public virtual Kategori Kategori { get; set; }
        public int?  KategoriId { set;get;}  // This is the new property
    }
    

    现在在您的视图中的表单中,确保由DropDownList 帮助器呈现的选择元素具有与新属性名称相同的name 属性值(检查页面的视图源)

    @Html.DropDownList("KategoriId", ViewData["Kategori"] as SelectList)
    

    现在,最后,确保在 Bind 属性 Include 列表中包含这个新的输入名称/属性名称,以便模型绑定器将其绑定。

    public async Task<IActionResult> Create([Bind(Include="Titel,Text,KategoriId")]
                                                                            Inlagg inlagg)
    {
        // to do : your code for saving and returning something
    }
    

    另一种选择是使用仅包含所需属性的视图模型,而不是在实体类中使用 Bind 属性

    【讨论】:

    • 仍然为空.. 认为我错过了创建中的某些内容?
    • 也不能使用 ([Bind/Includes=...}] 我只能使用:public async Task Create([Bind(include:"Titel,Text,Kategori.Namn" )] Inlägg inlägg)
    • 检查请求(表单帖子)正文并查看您有一个名为KategoryId 的项目。它应该在那里。您也需要在 Bind 包含列表中使用相同的内容。
    • 我试过这个:@Html.DropDownListFor(model => model.Kategori.Namn, ViewBag.Kategori as SelectList) @Html.HiddenFor(model => model.Kategori.Namn)-- ------ 但它添加到错误的表中,该值正在获取 Categori.Name 。例如:Inlägg 中的类别得到 9 而不是 2。在另一个表(类别)Id i 9 中添加了一个值,名称为 2。
    • 您需要遵循答案中的内容。您需要使用 KategoriId ,而不是 .Kategori.Namn
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    相关资源
    最近更新 更多