SOS! What I am trying to achieve is for the logged in person, to see the users he or she is supporting (buddying). I am now trying to fully embrace ViewModels to coagulate views. I am using simplemembership MVC4 with Mysql. I have a UserProperties(all details of my users)linked to Userprofile and Everything else works. I usually use two databases one for membership and another for all other stuff.
models
UserProfile/UserProPerties - extended for all 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
// I believe this is where the magic should come from
public ActionResult Index(int? id)
{
//I get 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 = from u in db.UserProperties
join m in db1.Buddyships on u.UserId equals m.BuddiedByUserId
where m.BuddiedByUserId == user.UserId
select new { u.FirstName, u.LastName, u.SchoolName, u.UserId };
var buddyviewmodel = new BuddyViewModel(buds //don't know what to 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 you are able to help
Try to change your code as follows.
ViewModel:
Your action:
Change View. In your action you are sending only one BuddyViewModel, which contains a list of UserProperties, but not a list of BuddyViewModels