Asp.net mvc 4 MVC4中ViewBag中的可枚举对象

Asp.net mvc 4 MVC4中ViewBag中的可枚举对象,asp.net-mvc-4,razor,Asp.net Mvc 4,Razor,我有以下型号,如F: public partial class F { [Key, Display(Name = "Id")] public int FId { get; set; } public int RId { get; set; } public int FTId { get; set; } public string C { get; set; } public string U { get; set; } publ

我有以下型号,如F:

public partial class F 
{
    [Key, Display(Name = "Id")]
    public int FId { get; set; }

    public int RId { get; set; }

    public int FTId { get; set; }

    public string C { get; set; }

    public string U { get; set; }

    public string D { get; set; }

    [ScaffoldColumn(false)]
    public System.DateTimeOffset Created { get; set; }

}
在控制器中,我必须从数据库中读取“F”的所有记录,并将这些记录分配给可枚举的记录列表

例如:

 ViewBag.Cs = enumerable C column items (textbox)
 ViewBag.Us= enumerable U column items (textbox)
 ViewBag.FTIDs = enumerable FTId column items (this has to be a dropdown)
在我的生活中,我必须表现出来

 @Html.Textbox(Cs);
 @Html.Dropdown(FTIDs);
我只给出了textbox和dropdows作为示例,可能还有很多其他控件,如日期时间、复选框等。, 我应该能够在viewbag中将每个列作为列表写入,并在MVC视图中显示

有人能告诉我这是否可以实现以及如何实现吗


非常感谢…

不要将viewbag用于此类内容,而是将视图与模型紧密绑定。只有当你有很小的东西要通过时,才使用观景袋。。任何复杂的东西都应该始终使用强类型视图模型,这样就可以获得intellisence,并且对于单元测试来说,它必须更干净

视图模型:

Public class MyViewModel
{
      public List<F> MyListOfFObjects { get; set; }
}
现在在视图中,您可以迭代该视图模型

@foreach(var fObject in Model)
{
  @Html.TextBoxFor(m => m.fId)
  @Html.TextBoxFor(m => m.rID)
}
这里有一个链接,指向可以使用的不同@Html帮助程序列表

强绑定视图的参考:

public ActionResult Index()
{
  MyViewModel vm = new MyViewModel();
  // Initialize your view model
  // Get all the F objects from the database and populate the list

  return View(vm); // now your view will have the view model
}
@foreach(var fObject in Model)
{
  @Html.TextBoxFor(m => m.fId)
  @Html.TextBoxFor(m => m.rID)
}