c# - 在创建 View 中从数据库中填充下拉列表

标签 c# asp.net-mvc asp.net-mvc-4

如何在 mvc razor View 的一个 View 中的特定时间获取和插入数据?我的意思是在创建 View 中从数据库中填写一个下拉列表。

我要在添加主题和骗子模型时填写以下内容。

  • 部门列表
  • 学期列表
  • 标准 list
  • 流列表

作弊模型:

namespace firstapp.Models
{
  public class chepter
  {
      [ForeignKey("dip_id")]
      public int dipart_id { get; set; }
      public int chep_id { get; set; }
      public string subject { get; set; }
      public string chepter { get; set; }
      public List<dipartment> dipartlist { get; set; }
      public List<dipartment> stdlist { get; set; }
      public List<dipartment> semlist { get; set; }
      public List<dipartment> stremlist { get; set; } 
   }
} 

部门模型:

namespace firstapp.Models
{
   public class dipartment 
   {        
      public int dip_id { get; set; }
      public string dipart { get; set; }
      public string std { get; set; }
      public string sem { get; set; }
      public string strem { get; set; }
   }
}

最佳答案

@Html.DropDownListFor(model => model.dipart_id, new SelectList(Model.dipartlist.Select(s => new SelectListItem() { Value = s.dip_id, Selected = false, Text = s.dipart })), "Select")

关于c# - 在创建 View 中从数据库中填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24285901/

相关文章:

c# - 自动映射器性能

c# - 使用 "yield"时,为什么编译器生成的类型同时实现 IEnumerable 和 IEnumerator

c# - 网站地址验证的正则表达式

c# - 访问库函数时 char** 的编码问题

html - ASP.NET MVC 中 CSS 中的根目录

asp.net-mvc - 如何本地化/更改 DataAnnotation 的必填字段验证器?

css - Bootstrap 不包括任何字形

c# - 发送大型 XML 时 IIS 7.5 崩溃

c# - 为什么我的 ASP.NET 项目没有加载 Dll?

c# - MVC 将我的 GET 请求路由到错误的操作