Reputation: 407
I'm creating a simple website in ASP.NET MVC.
There is a list of events where users can subscribe to with a specific amount. (transactions)
Short description: I want to make user based money management. People can topup their account with money. (sum of transation table, amount). For every event they can subscribe for an amount. This is also a transaction but now specific for an event. The total balance of the user can be find out by the sum of transactions table.
UserModel
[Required]
[Key]
public virtual int UserId { get; set; }
[Required]
[EmailAddress]
public virtual string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual bool Admin { get; set; }
EventModel
[Required]
[Key]
public virtual int EventId { get; set; }
[Required]
public virtual string Title { get; set; }
public virtual string Description { get; set; }
[Required]
public virtual DateTime StartDate { get; set; }
[Required]
public virtual DateTime EndDate { get; set; }
TransactionModel
[Required]
[Key]
[Column(Order = 1)]
public virtual int TransactionId { get; set; }
[Required]
[Key]
[Column(Order = 2)]
public virtual int UserId { get; set; }
public virtual int? EventId { get; set; }
[Required]
public virtual string Description { get; set; }
[Required]
public virtual double Amount { get; set; }
public virtual UserModel User { get; set; }
public virtual EventModel Event { get; set; }
So the main thing is that there is a event. Where users can join in the event and what specific amount they want to spend. It's not necessarily needed to fill in a EventId in the transaction. Think about a TOPUP where you just add a amount to the balance of the current user.
If I get the details of an event, the transactions need appear in a list. In my controller I do the following:
// GET: Event/Details/5
public ActionResult Details(int? id)
{
var transactions = db.Transactions.Include(t => t.Event).Include(t => t.User).Where(x => x.EventId == id).ToList();
if (transactions == null)
{
return HttpNotFound();
}
return View(transactions);
}
And my view is looking like this:
@model IEnumerable<MyBalance.Models.TransactionModel>
Html.DisplayFor(model => model.First().Event.Title);
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.User.FirstName)</td>
<td>@Html.DisplayFor(modelItem => item.Description)</td>
<td>@Html.DisplayFor(modelItem => item.Amount)</td>
</tr>
}
This is working IF there is any transaction. But this is not working if I don't have any transaction.
I think i'm doing something wrong and my relationship is not well implemented. I think my controller should look like:
var events = db.Events.Include(t => t.Transaction).Where(x => x.EventId == id).ToList();
Upvotes: 1
Views: 89
Reputation: 1326
If you want to show Event Details with its transactions list then you need to add a navigation collection property for Transactions in EventModel like this:
public virtual ICollection<TransactionModel> Transactions { get; set; }
then you can query like this:
var eventt = db.Events.Include(t => t.Transactions).Where(x => x.EventId == id).FirstOrDefault();
it will return an event with all its transactions.
Then in view you can use like this:
@model MyBalance.Models.Event
<h2>Model.Title</h2>
@foreach(var item in Model.Transactions)
{
<tr>
<td>@item.Description</td>
<td>@item.Amount</td>
</tr>
}
Upvotes: 1
Reputation: 1326
.ToList() will not return null value, it will return Count=0 so it should be like this:
public ActionResult Details(int? id)
{
var transactions = db.Transactions.Include(t => t.Event).Include(t => t.User).Where(x => x.EventId == id).ToList();
if (transactions == null || transactions.Count==0)
{
return HttpNotFound();
}
return View(transactions);
}
Upvotes: 0