gpt4 book ai didi

html - 如何在当前模型部分添加下拉 YearID

转载 作者:行者123 更新时间:2023-11-28 04:46:43 25 4
gpt4 key购买 nike

我需要什么

我需要添加下拉菜单和 LabelFor YearID 并从模型 Year 添加到当前模型部分与 Formate bootstrap 为(将 departID 替换为 YearID)。

我在 MVC 5 visual studio 2015 工作,我需要从另一个模型中获取下拉列表。

当前模型名称是 Section:

namespace UniversityData.Models  
{

[Table("Section")]
public partial class Section
{
public Section()
{

}
[Key]
public int SecID { get; set; }

[Required]
[StringLength(50)]
public string SecName{ get; set; }

[ForeignKey("Department")]
[DisplayName("DepartmentName")]
public int departID { get; set; }
}
}

我使用 bootstrapis 在当前模型部分中下拉 departID 的代码:

<div class="form-group">  
@Html.LabelFor(model => model.departID, "DepartmentName", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("departID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.departID, "", new { @class = "text-danger" })
</div>
</div>

我想为其生成下拉列表的另一个模型年份

 [Table("Year")]  
public partial class Year
{
public Year()
{
this.department = new HashSet<Department>();

}
[Key]
public int YearID { get; set; }
[Required]
[StringLength(50)]

public string YearName { get; set; }

}
}

我尝试如下:

  <div class="form-group">

//如何添加用于替换文本的标签

           YearName:
<div class="col-md-10">
@Html.DropDownList("YearID", null, htmlAttributes: new { @class = "form-control" })

</div>
</div>

我收到错误:

{"DataBinding: 'System.Data.Entity.DynamicProxies.Department_3D49A8148DA2B6C2F8801CE72951BF7FDBF82503B3A52665397F8B7058E3F8A8' does not contain a property with the name 'YearName'."}

最佳答案

谢谢你解决了我的问题

行动中发现的问题:

  ViewBag.YearID = new SelectList(db.years.ToList(), "YearID", "YearName");

在它找到 db.departments.ToList() 之前,所以在我将 departments 更改为 years 问题解决后出现错误,因为 drop down for years not department 。

在我看来,我做了以下事情:

 <div class="form-group">

@Html.Label("YearName", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("YearID", null, htmlAttributes: new { @class = "form-control" })

</div>
</div>

关于html - 如何在当前模型部分添加下拉 YearID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40920920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com