0

我有一个 ViewModel,它显示 Razor 表中的值列表。我想添加一个下拉列表来在显示表格数据之前对其进行过滤。我发现了各种关于将 ddl 绑定到列表的帖子,但它们都建议 DDL 控件的格式为 @Html.DropDownListFor(x => Model.field)。不幸的是,当我输入 Model. 我的 InteliSense 预输入列表不显示我的视图模型列表。

任何帮助将不胜感激。

我的模型是:

public class HoursView
    {
        [Key]
        public int HoursID { get; set; }
        [Display(Name = "Company ID:")]
        public int CompanyID { get; set; }
        public string CompanyName { get; set; }
        public int EmployeeID { get; set; }
        public string EmployeeName { get; set; }
        [Display(Name = "Week Ending Date:")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
        [Required]
        public DateTime? WeekEndingDate { get; set; }
        [Display(Name = "Hours Worked:")]
        [Required]
        public decimal? HoursWorked { get; set; }
        [Display(Name = "Tips:")]
        [Required]
        public decimal? Tips { get; set; }
        [Display(Name = "Tips-Cash:")]
        [Required]
        public decimal? Tips_Cash { get; set; }
        public IEnumerable<SelectListItem> Companies { get; set; }
    }

我的控制器“GET”代码是:

var requiresHours =
                from company in _context.Company
                join employee in _context.Employees
                on company.CompanyID equals employee.CompanyID
                join hours in _context.EmployeeHours
                on employee.EmployeeID equals hours.EmployeeID
                into hourEmployeees
                from subHours in hourEmployeees.DefaultIfEmpty()
                where (employee.EmployeeType == "Hourly" || employee.EmployeeType == "Tips") &&
                        (employee.EmployeeStatus == "Active")
                orderby employee.CompanyID, employee.EmployeeID
                select new HoursView
                {
                    CompanyID = company.CompanyID,
                    CompanyName = company.CompanyName,
                    EmployeeID = employee.EmployeeID,
                    EmployeeName = employee.EmployeeName,
                    WeekEndingDate = subHours == null ? dateEndingWeek : subHours.WeekEndingDate,
                    HoursWorked = subHours == null ? 0 : subHours.HoursWorked,
                    Tips = subHours.Tips == null ? 0 : subHours.Tips,
                    Tips_Cash = subHours.Tips_Cash == null ? 0 : subHours.Tips_Cash,
                    Companies = (from company in _context.Company select new SelectListItem
                    {
                        Value = company.CompanyID.ToString(),
                        Text = company.CompanyName
                    }).ToList()
                };
                return View(await requiresHours.ToListAsync());

我的 Razor 代码是(包括不起作用的行):

@model IEnumerable<MvcPayroll.Models.HoursView>

@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>

@using (Html.BeginForm("Index","EmployeeHours",FormMethod.Post))
{
    @Html.DropDownListFor(x => Model. 
    

    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.CompanyID)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.EmployeeID)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.WeekEndingDate)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.HoursWorked)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Tips)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Tips_Cash)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.CompanyName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.EmployeeName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.WeekEndingDate)
                    </td>
                    <td>
                        @Html.EditorFor(modelItem => item.HoursWorked)
                    </td>
                    <td>
                        @Html.EditorFor(modelItem => item.Tips)
                    </td>
                    <td>
                        @Html.EditorFor(modelItem => item.Tips_Cash)
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <p><input type="submit" value="Save" /></p>
    <p style="color:green; font-size:12px;">
        @ViewBag.Message
    </p>
}

谢谢,大卫

4

1 回答 1

0

我永远无法让它发挥作用。我求助于使用 ViewBag 与集合。这是在有关在此站点上填充下拉列表主题的许多帖子中推荐的。

于 2021-12-08T01:01:47.893 回答