简体   繁体   English

如何在Razor中显示下拉列表

[英]How to show a dropdown list in Razor

I'm having some troubles with Models and ViewModels. 我在Models和ViewModels上遇到了麻烦。

I need list all bills and the users in the same view. 我需要在同一视图中列出所有账单用户 (users in a dropdown list) (下拉列表中的用户)

Also: When to use IEnumerable<T> ? 另外:什么时候使用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 have some issues in sample code provided: 您提供的示例代码中存在一些问题:

1) The DropDownListFor uses User.Identity.Name property as model binding, which actually derived from IIdentity.Name which is getter-only property. 1) DropDownListFor使用User.Identity.Name属性作为模型绑定,它实际上是从IIdentity.Name派生的,后者是仅吸气剂的属性。 Declare another property with setter available which holds user ID in your viewmodel. 声明另一个可用的setter属性,该属性将用户ID保留在视图模型中。

2) Passing UserBills viewmodel into view which bound to IEnumerable<UserBills> model may cause InvalidOperationException . 2)将UserBills视图模型传递到绑定到IEnumerable<UserBills>模型的视图中可能会导致InvalidOperationException You need to use either passing IEnumerable<UserBills> from controller or define @model UserBills . 您需要使用从控制器传递IEnumerable<UserBills>或定义@model UserBills

3) I suggest you use IEnumerable<SelectListItem> to create DropDownListFor items from IEnumerable<ApplicationUser> generated by identity data context and pass it to view (see also IdentityUser properties ). 3)我建议您使用IEnumerable<SelectListItem>从身份数据上下文生成的IEnumerable<ApplicationUser>创建DropDownListFor项目,并将其传递给视图(另请参见IdentityUser属性 )。

Here is initial solution based from my thought: 这是基于我的想法的初始解决方案:

Model 模型

public class UserBills
{
    public int UserId { get; set; }
    public IEnumerable<SelectListItem> Users { get; set; }
    public IEnumerable<Bill> Bills { get; set; }
}

Controller 调节器

public ActionResult Index()
{
    var users = _context.Users.ToList();
    var bills = _context.Bills.ToList();
    var viewModel = new UserBills
    {
        Users = users.Select(x => new SelectListItem() { Value = x.Id.ToString(), Text = x.UserName.ToString() }),
        Bills = bills
    }

    return View(viewModel);    
}

View 视图

@model Nucontrol.ViewModels.UserBills

@foreach (var item in Model.Bills)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Card.Number)
        </td>
        <!-- other properties -->
    </tr>
}

@Html.DropDownListFor(m => m.UserId, Model.Users, "", new { @class = "form-control" })

NB: Since you're getting selected user ID from viewmodel binding, it is possible to create HttpContext.User instance and setting User.Identity.Name property from that ID. 注意:由于从视图模型绑定中选择了用户ID,因此可以创建HttpContext.User实例并从该ID设置User.Identity.Name属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM