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

标签 html twitter-bootstrap css asp.net-mvc-4 entity-framework-6

我需要什么

我需要添加下拉菜单和 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/

相关文章:

javascript - 一屏显示图片的CSS/Javascript框架

html - 如何在div上创建带背景的多色顶部边框?

css - 需要下拉菜单在页面加载时具有一定的宽度,但在单击时显示完整的内容长度

html - Safari 下部的选择框无法选择

html - HTML5 中的链接标签不应自动关闭,但 HAML 正在关闭它们

html - 使用 Bootstrap 4 将标签元素内的图标定位到右侧

html - 无法让 CSS 在顶部/底部提供与侧面相同的填充

css - Bootstrap - 具有相同高度和宽度的面板

css - Bootstrap 行中的间距和填充/边距

javascript - 在部分中居中旋转木马(css 3d)