I'm having some troubles with Models and ViewModels.
I need list all bills and the users in the same view. (users in a dropdown list)
Also: When to use IEnumerable<T>
? Because depending I change the view change the error message.
Model
public class Bill
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime Date { get; set; }
public string Category { get; set; }
public double Amount { get; set; }
public Card Card { get; set; }
public int CardId { get; set; }
}
ViewModel
public class UserBills
{
public IEnumerable<ApplicationUser> User { get; set; }
public Bill Bill { get; set; }
}
View
@*@model IEnumerable<Nucontrol.Models.Bill>*@
@model IEnumerable<Nucontrol.ViewModels.UserBills>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Bill.Card.Number)
</td>
<td>
@Html.DisplayFor(modelItem => item.Bill.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Bill.Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.Bill.Category)
</td>
<td>
@Html.DisplayFor(modelItem => item.Bill.Amount)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Bill.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Bill.Id }) |
@Html.ActionLink("Split", "Split", new { id = item.Bill.Id }, new { data_target = "#myModal", data_toggle = "modal" })
</td>
</tr>
}
<!-- List all users -->
@Html.DropDownListFor(m => User.Identity.Name, new SelectList(User.Identity.Name, "Id", "Name"), "", new { @class = "form-control" })
Controller
public ActionResult Index()
{
var users = _context.Users.ToList();
var bills = _context.Bills.ToList();
var viewModel = new UserBills
{
User = users
};
return View(viewModel);
}