c# - Repository w/ View Model returning null on one property -
i’ve searched number of hours, can’t find i’m looking answer wise. have following:
(repository.cs)
public class apprespository { private datacontext db = new datacontext(); public ienumerable<selectlistitem> loadstates() { var query = d in db.states.tolist() select new selectlistitem { value = d.stateid.tostring(), text = d.state.tostring() }; return query; } }
datacontext.cs
public class datacontext : dbcontext { public dbset<states> states { get; set; } }
viewmodel (states.cs)
public class states { [key] public int stateid { get; set; } public ienumerable<selectlistitem> state { get; set; } }
controller (applicantcontroller.cs)
// get: /applicant/ public actionresult index() { apprespository repo = new apprespository(); states viewmodel = new states(); viewmodel.state = repo.loadstates(); return view(viewmodel); }
view:
@html.dropdownlistfor(model=>model.stateid, model.state, "select")
why when debugging application null on line repository class?
text = d.state.tostring(),
state null, while stateid getting set. have loaded drop down before using model , controller, pattern. i'm intermediate w/ asp mvc i'm missing something.
any appreciated.
looks need move things around bit here. i'm making assumptions want achieve,
(repository.cs)
public class apprespository { private datacontext db = new datacontext(); public ienumerable<selectlistitem> loadstates() { var query = d in db.states.tolist() select new selectlistitem { value = d.stateid.tostring(), text = d.state }; return query; } }
dbcontext
public class datacontext : dbcontext { public dbset<stateentity> states { get; set; } }
state entity
public class stateentity { [key] public int stateid { get; set; } public string state { get; set; } }
viewmodel
public class statemodel { public ienumerable<selectlistitem> states {get; set;} }
controller action
// get: /applicant/
public actionresult index() { apprespository repo = new apprespository(); statemodel viewmodel = new statemodel(); viewmodel.states = repo.loadstates(); return view(viewmodel); }
hopefully helps of way there. i'm assuming want load list of int / string pairs stateentity
Comments
Post a Comment