Below is the exact code snippet of how you would bind data in DropDownList from two tables in mvc using Entity Framework.DropDownList Code
@Html.DropDownList("MultipleTables", ViewBag.Roles as SelectList) Action Method code of the controller
public ActionResult Create()
var data = from p in db.PersonalDetails
join f in db.Files
on p.AutoId equals f.PersonalDetailsId
PersonName = p.FirstName,
MyFileName = f.FileName
SelectList list = new SelectList(data, "MyFileName", "PersonName");
ViewBag.Roles = list;
In the above code snippet, we are joining PersonalDetails and Files tables using LINQ (Know more about join in LINQ http://www.dotnetfunda.com/codes/show/1849/how-to-use-left-right-outer-join-in-linq
and getting FirstName as PersonName and FileName as MyFileName property of the object.
The same is being converted into SelectList by specifying the dataTextField and dataValueField and then set as ViewBag.Roles
This ViewBag.Roles is being given as data source of the Html.DropDownList in the View (first code snippet).
Hope this helps.
ThanksShail, if this helps please login to Mark As Answer. | Alert Moderator