c# - How can i get the correct information to display in my listbox? -
i have 2 listboxes can move exercises available list of exercises list of selected exercises specific user. current problem selected list (aka regimeitems) shows exercises of users.
i believe problem lies in chosenexercises(at bottom of controller) getting null reference exception at:
var regimeids = model.savedrequested.split(',').select(x => int.parse(x));
controller.cs
[httpget] public actionresult exerciseindex(int? id, userexerciseviewmodel vmodel) { user user = db.users.find(id); user.regimeitems = chosenexercises(); userexerciseviewmodel model = new userexerciseviewmodel { availableexercises = getallexercises(), requestedexercises = chosenexercises(user, vmodel) }; user.regimeitems = model.requestedexercises; return view(model); } [httppost] public actionresult exerciseindex(userexerciseviewmodel model, string add, string remove, string send, int id) { user user = db.users.find(id); user.regimeitems = model.requestedexercises;; restoresavedstate(model); if (!string.isnullorempty(add)) addexercises(model, id); else if (!string.isnullorempty(remove)) removeexercises(model, id); savestate(model); return view(model); } void savestate(userexerciseviewmodel model) { model.savedrequested = string.join(",", model.requestedexercises.select(p => p.regimeitemid.tostring()).toarray()); model.availableexercises = getallexercises().tolist(); } void restoresavedstate(userexerciseviewmodel model) { model.requestedexercises = new list<regimeitem>(); //get stored items if (!string.isnullorempty(model.savedrequested)) { string[] exids = model.savedrequested.split(','); var exercises = getallexercises().where(p => exids.contains(p.exerciseid.tostring())); model.availableexercises.addrange(exercises); } } private list<exercise> getallexercises() { return db.exercises.tolist(); } private list<regimeitem> chosenexercises(user user, userexerciseviewmodel model)//regimeitem regimeitem) { var regimeids = model.savedrequested.split(',').select(x => int.parse(x)); return db.regimeitems.where(e => regimeids.contains(e.regimeitemid)).tolist(); //return db.users.where(r => r.regimeitems = user.userid); //return db.users.where(r => r.regimeitems.regimeitemid == user.userid); }
models
public class user { public int userid { get; set; } public icollection<regimeitem> regimeitems { get; set; } public user() { this.regimeitems = new list<regimeitem>(); } } public class regimeitem { public int regimeitemid { get; set; } public exercise regimeexercise { get; set; } }
viewmodel
public class userexerciseviewmodel { public list<exercise> availableexercises { get; set; } public list<regimeitem> requestedexercises { get; set; } public int? selectedexercise { get; set; } public int[] availableselected { get; set; } public int[] requestedselected { get; set; } public string savedrequested { get; set; } }
try changing
return db.users.where(r => r.regimeitems = user.userid);
to
return db.users.where(r => r.regimeitems.regimeitemid == user.userid);
because way read you're trying match int object.
edit: error resulted op found himself.
"it seems regimeitems collection of regimeitem - not have have single id. needed .where(u => u.userid == user.userid) , select regimeitems associated user .selectmany(u => u.regimeitems).tolist(); help, if update answer mark yours correct answer." -nilmag
Comments
Post a Comment