asp.net mvc 4 - loading dropdown list with data from table in mvc4 -
i have 3 models , corresponding tables (enquirymodel, employeemodel, regionmodel). want populate dropdown list in both 'create' views of enquiry , employee. dropdown list should filled data 'region_name' field in regionmodel table.
how can this?
i have regionmodel:
namespace mvcconquery.models { [table("region_details")] public class regionmodel { [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int32 region_id { get; set; } public string region { get; set; } } }
and have employeemodel , enquirymodel, give following code in controller of these models:
public actionresult create() { ienumerable<selectlistitem> abc = new selectlist(db.regions, "region_id", "region"); viewbag.regions = abc; return view(); }
i give code in create view:
@html.dropdownlist("regions","select")
but got error:
there no viewdata item of type 'ienumerable' has key 'regions'.
please me code create()function in controller , create view.
try this,
dataentities _context = new dataentities (); var list = (from r in _context.registrations select r).tolist(); viewbag.customername = new selectlist(list, "region_id", "region");
view
@html.dropdownlist("regions", (selectlist)viewbag.customername, "--select--")
if have concerned let me know.
Comments
Post a Comment