c# - Dropdownlists not populating with data from 3 separate reference tables -
currently working asp.net mvc 6 using ef7. using default controller generator. reason drop downs not populating data on create or edit page though data present.
just clarify 3 select lists being populated 3 different tables connected main table adding to.
here's got.
controller code
private readonly schoolcontext _context; public schoolscontroller(schoolcontext context) { _context = context; } public iactionresult create() { viewdata["districtid"] = new selectlist(_context.districts, "districtid", "district"); viewdata["locationid"] = new selectlist(_context.locations, "locationid", "location"); viewdata["tierid"] = new selectlist(_context.tiers, "tierid", "tier"); return view(); }
view code
@model school
is included @ top , here 1 of select element looks like
<div class="form-group"> <label asp-for="districtid" class="col-md-2 control-label"></label> <div class="col-md-10"> <select asp-for="districtid" class ="form-control"></select> </div> </div>
the select lists blank no errors there data.
this generated automatically totally clueless on went wrong. suggestions?
you need both property bind (the selected value) , property options , need have different names. ideally should using view model have (say)
public class schoolvm { public int districtid { get; set; } public ienumerable<selectlistitem> districtlist { get; set; } ....
and in method
public iactionresult create() { schoolvm model = new schoolvm { districtlist = new selectlist(_context.districts, "districtid", "district"), ..... }; return view(model); }
and in view
@model schoolvm .... <select asp-for="districtid" asp-items="model.districtlist"></select>
alternatively, use viewbag
or viewdata
, use
<select asp-for="districtid" asp-items="viewbag.districtlist"></select>
Comments
Post a Comment