1

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();
8
  • What is .Include(t => t.TransactionStatus)? - it does not appear to be a property of your model. Commented Oct 29, 2015 at 10:08
  • Oh sorry, I snipped this out for keeping it simple. I will edit this. This is a little table for a status of the transaction. "Pending", "Approved", "Cancelled". Commented Oct 29, 2015 at 10:17
  • So what is not working? Are you getting an exception? Commented Oct 29, 2015 at 10:18
  • I assume its Html.DisplayFor(model => model.First().Event.Title);? Commented Oct 29, 2015 at 10:19
  • I'm not getting an exception if there is no transaction for the event. I want to show in my detail the event information AND to show the current transactions for this event. But if there isn't a transaction the List is empty. I'm doing something wrong in relationship I guess. Commented Oct 29, 2015 at 10:20

2 Answers 2

1

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>
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Ankit. It's pretty much the same answer as Stephan (in comments). This is exacly what I need.
I'm back again. This solution is working pretty fine. But on the same page I want to add a transaction inside the view (form). How can I do this? I can't add TransactionModel to the view because i'm using EventModel.
On your view as EventModel is Model so for accessing TransactionModel you can use foreach on Model.Transactions (It contains TransactionModel objects)
And outside the foreach statement? I need a single form with Description and Amount to be added to the event. @using (Html.BeginForm("AddTransaction", "Event", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "reg-page" })) { @Html.TextBoxFor(t => t.Transactions.FirstOrDefault().Amount); <p><input type="submit" value="Test" class="btn-u pull-right" /></p> } is showing the first value of the record that already exists but it needs to be a new transaction item.
0

.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);
    }

2 Comments

Thanks for your reaction. But the main thing is that I want to show Event details AND the current transactions. But how do I show them both? Even if there are no transactions I want to show what event this is.
please check my second answer for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.