Home How to show a dropdown list in Razor
Reply: 0

How to show a dropdown list in Razor

user2295
1#
user2295 Published in June 19, 2018, 4:15 pm

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);
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.418929 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO