C# MVC和下拉列表

C# MVC和下拉列表,c#,asp.net-mvc,C#,Asp.net Mvc,我想在我的网页中包含我的对象(从数据库读取)的下拉列表 这是我的目标: public class MyObject { public int id { get; set; } public string fileName { get; set; } public string browser { get; set; } public string protocol { get; set; } public string family { get; s

我想在我的网页中包含我的对象(从数据库读取)的下拉列表

这是我的目标:

public class MyObject
{
    public int id { get; set; }
    public string fileName { get; set; }

    public string browser { get; set; }

    public string protocol { get; set; }

    public string family { get; set; }
}
我的控制器:

public ActionResult Index()
{
    List<MyObject> list = db.MyObjects.Where(x => x.family == "Web").ToList();
    ViewBag.Files = lList;
    return View();
}
public ActionResult Index()
{
List List=db.MyObjects.Where(x=>x.family==“Web”).ToList();
ViewBag.Files=lList;
返回视图();
}
Index.cshtml

@model IEnumerable<MyApplication.Models.MyObject>
@{
    ViewBag.Title = "Index";    
}
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
不知道如何从这里继续。

试试这个

public ActionResult Index()
{
    List<MyObject> list = db.MyObjects.Where(x => x.family == "Web").ToList();
    ViewBag.Files =new SelectList(list,"Id","fileName)";
    return View();
}

附加信息:对象引用未设置为对象的实例,无法理解为什么编辑:好,工作正常,但如果我想在DropdownList中仅显示myObject属性的特定内容?(当然,如果有,请避免重复)您可以尝试在controller中进行DistinctBy,因为这更合适。或者更确切地说,
@Html.DropDownList(“文件”,ViewBag.Files as SelectList)
如果您可以提供一些示例数据,那么它将有所帮助
  @Html.DropDownList("File",new SelectList(ViewBag.Files))