I’m beginning out with asp.net mvc3 and mvc in general and I’m learning entity framework at the same time also.
The app i’m building allows users to create bills or payments for other users and I am unsure on how to create the relationships for the bill and account classes when writing my query. The issue i’m having is I am storing the accountId’s in the database however I want to display the user’s first and last name from the toAccountId and fromAccountId.
Classes
public class Bill
{
public int BillId { get; set; }
public string BillName { get; set; }
public string BillDescription { get; set; }
public decimal Price { get; set; }
public DateTime DateCreated { get; set; }
public DateTime? DateDue { get; set; }
public int FromAccountId { get; set; }
public int ToAccountId { get; set; }
}
public class Account
{
public int AccountId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MobilePhone { get; set; }
public string BankAccountNumber { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public DateTime LastLogin { get; set; }
}
public class HomeViewModel
{
public IEnumerable<Bill> Bills { get; set; }
}
In the controller
private IBillRepository repository;
public ViewResult Home()
{
int userID = 1;
HomeViewModel viewModel = new HomeViewModel
{
Bills = repository.Bills
.OrderByDescending(b => b.DateCreated)
.Where(b => b.FromAccountId.Equals(userID))
.Take(amountOfBillsOnPageOnLoad)
};
return View(viewModel);
}
this might help // using the navigation property
in your view you can access the Account object properties like this.