user979033
user979033

Reputation: 6490

MVC and DropdownList

I want to include a drop down list of my object (read from database) across my webpage.

This is my object:

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

My controller:

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

Index.cshtml

@model IEnumerable<MyApplication.Models.MyObject>
@{
    ViewBag.Title = "Index";    
}

Don't know how to continue from here.

Upvotes: 0

Views: 86

Answers (1)

Miller
Miller

Reputation: 1156

Try This

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

Add in the view

  @Html.DropDownList("File",new SelectList(ViewBag.Files))

Upvotes: 1

Related Questions