【问题标题】:How to add dropdown YearID in current model section如何在当前模型部分添加下拉 YearID
【发布时间】:2016-12-01 21:42:40
【问题描述】:

我需要什么

我需要添加下拉菜单和 LabelFor YearID 并从模型 Year 添加到 当前模型部分,使用 Formate 引导程序作为(用 YearID 替换离开 ID)。

我在 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 在我当前的模型部分中下拉离开 ID:

<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’不包含名为‘YearName’的属性。”}

【问题讨论】:

  • 谁能帮帮我

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


【解决方案1】:

谢谢你解决了我的问题

行动中发现的问题:

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

在它发现为 db.departments.ToList() 之前,因此在我将部门更改为年份问题解决后出现错误,因为下拉年份不是部门。

鉴于我的做法如下:

 <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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-07
    • 2019-01-20
    • 2015-07-08
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 2013-11-10
    相关资源
    最近更新 更多