I have created mvc3 application.
where using EF to populate dropdownlist from tables.
now I have two dropdownlists on my index.cshtml each populating from different tables.
code index.cshtml:
i have taken model as entity as it is having both my tables table1 and table2
@model Mapping.Models.mydataEntities1
but problem is here i’m not able to select values from both tables. 🙁
like following code is for only working when i give
model @model Mapping.Models.table1
but need to select values into two dropdown from different tables
@using (Html.BeginForm())
{
@Html.DropDownListFor(
x => x.CategoryId,
new SelectList(Model.Categories, "Value", "Text"),
"-- Select category --"
)
<input type="submit" value="OK" />
}
How can i do this?
I have used EF
public partial class mydataEntities1 : DbContext
{
public mydataEntities1()
: base("name=mydataEntities1")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<table1> table1 { get; set; }
public DbSet<table2> table2 { get; set; }
}
}
I’m not an EF user, but assuming that your model is populated and works correctly
the following code should works
But I strongly reccomend you to create a ViewModel to expose your data to the View and not your domain entities or directly the repository.