asp.net mvc 4 - Buddy ViewModel with LINQ Join -
sos! trying achieve logged in person, see users or supporting (buddying). trying embrace viewmodels coagulate views. using simplemembership mvc4 mysql. have userproperties(all details of users)linked userprofile , else works. use 2 databases 1 membership , other stuff.
models
userprofile/userproperties - extended other properties
- userid
- list item
- username
userproperty
- firstname
- lastname
- schoolname
- userid
buddyship
- buddyid
- buddiedbyuserid
- buddieduserid
viewmodel model
public class buddyviewmodel { public buddyship buddyship {get; set;} public list<buddyship> allbudees {get; set;} public userproperty userproperty { get; set; } public panelviewmodel(buddyship buddyship, list<buddyship> allbudees) { buddyship = buddyship; allbudees = allbudees; } }
buddyviewmodel controller
// believe magic should come public actionresult index(int? id) { //i logged in user properties var user = db.userproperties.singleordefault(x => x.username == user.identity.name); buddyship allbudees = db1.buddyships.singleordefault(u =>u.buddiedbyuserid == user.userid); var buds = u in db.userproperties join m in db1.buddyships on u.userid equals m.buddiedbyuserid m.buddiedbyuserid == user.userid select new { u.firstname, u.lastname, u.schoolname, u.userid }; var buddyviewmodel = new buddyviewmodel(buds //don't know put here); return view(buddyviewmodel); }
view
@model ienumerable<budtt.models.buddyviewmodel> @foreach (var item in model.buddyships) { <p>@html.displayfor(model =>model.userproperty.firstname)</p> <p>@html.displayfor(model =>model.userproperty.lastname)</p> }
thanks if able help
try change code follows.
viewmodel:
public class buddyviewmodel { public buddyship buddyship {get; set;} public list<buddyship> allbudees {get; set;} public list<userproperty> users { get; set; } //** add property }
your action:
public actionresult index(int? id) { //i logged in user properties var user = db.userproperties.singleordefault(x => x.username == user.identity.name); ///**************get buddyships of current user************************* list<buddyship> allbudees = db1.buddyships.where(u =>u.buddiedbyuserid == user.userid).tolist(); ///**************get users supporting user************************* var buds = (from u in db.userproperties join m in allbudees on u.userid equals m.buddieduserid m.buddiedbyuserid == user.userid select new userproperty { firstname = u.firstname, lastname = u.lastname, schoolname = u.schoolname, userid = u.userid }).tolist(); var buddyviewmodel = new buddyviewmodel { users = buds, allbudees = allbudees, //** if need property in view, ... } return view(buddyviewmodel); }
change view. in action sending 1 buddyviewmodel, contains list of userproperties, not list of buddyviewmodels
@model budtt.models.buddyviewmodel @foreach (var item in model.users) { <p>@item.firstname</p> <p>@item.lastname</p> }
Comments
Post a Comment