ASP.NET MVC 的月份和年份下拉列表

本文关键字:下拉列表 NET MVC ASP | 更新日期: 2023-09-27 18:36:05

我正在尝试在 mvc 项目的 cshtml 视图页面中填充月份和年份 asp.net

这是查看页面代码

@model IEnumerable<project.Models.ProductStatistics>
@{
 }
@Html.DropDownList("ExpirationMonth", ExpirationMonthDropdown)
@Html.DropDownList("ExpirationYear", ExpirationYearDropdown)

这是模型

public class ProductStatistics
{
    public IEnumerable<SelectListItem> ExpirationMonthDropdown
    {
        get
        {
            return Enumerable.Range(1, 12).Select(x =>
                new SelectListItem()
                {
                    Text = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames[x - 1] + " (" + x + ")",
                    Value = x.ToString(),
                    Selected = (x == Model.ExpirationMonth)
                });
        }
    }
    public IEnumerable<SelectListItem> ExpirationYearDropdown
    {
        get
        {
            return Enumerable.Range(DateTime.Today.Year, 20).Select(x =>
            new SelectListItem()
            {
                Text = x.ToString(),
                Value = x.ToString(),
                Selected = (x == Model.ExpirationYear)
            });
        }
    }
}

但在这里我在模型类中收到以下错误

名称"模型"在当前上下文中不存在

也在视图页面中收到此错误

名称"过期月下拉列表"在当前上下文中不存在

ASP.NET MVC 的月份和年份下拉列表

使用以下代码更改模型

public class ProductStatistics
{
   [Display(Name = "Product ID")]
    public string Product_ID { get; set; }
   [Display(Name = "Product Name")]
    public string ProductName { get; set; }
}
public class ProductStatisticsList
{
    public List<ProductStatistics> Products
    {
        get;
        set;
    }
    public int SelectedMonth
    {
        get;
        set;
    }
    public int SelectedYear
    {
        get;
        set;
    }
}

在行动中

public ActionResult Index()
{
   ViewBag.Months = new SelectList(Enumerable.Range(1, 12).Select(x =>
      new SelectListItem()
              {
                  Text = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames[x - 1] + " (" + x + ")",
                  Value = x.ToString()
              }), "Value", "Text");

        ViewBag.Years = new SelectList(Enumerable.Range(DateTime.Today.Year, 20).Select(x =>
           new SelectListItem()
           {
             Text = x.ToString(),
             Value = x.ToString()
           }), "Value", "Text");
      ProductStatisticsList p = new ProductStatisticsList();
     // p.Products = new List<ProductStatistics>();
      //p.Products.Add(new ProductStatistics { Product_ID = "Product_ID", ProductName = "ProductName" });
      p.Products = (from productstatistics in db.ProductStatistics
                    select new ProductStatistics
                    {
                        Product_ID = productstatistics.Product_ID,
                        ProductName = productstatistics.ProductName,
                    }).ToList();

        p.SelectedMonth = 3;
        return View(p);
 }

    [HttpPost]
    public ActionResult Index(ProductStatisticsList model)
    {
        //do the stuff
    }

在您看来

@model project_name.Models.ProductStatisticsList
@{
 }
  @using (Html.BeginForm("index", "Home", FormMethod.Post, new { id = "formIndex" }))
{
    @Html.DropDownListFor(model => model.SelectedMonth, (IEnumerable<SelectListItem>)ViewBag.Months, "Month") 
    @Html.DropDownListFor(model => model.SelectedYear, (IEnumerable<SelectListItem>)ViewBag.Years, "Year") 
    <table class="table">
        @if (Model.Products != null && Model.Products.Count > 0)
        {
            <tr>
                 <th>
                    @Html.DisplayNameFor(modelItem => Model.Products[0].Product_ID) @*This approch is wrong , should read from resource file*@
                </th>
                <th>
                    @Html.DisplayNameFor(modelItem => Model.Products[0].ProductName) @*This approch is wrong , should read from resource file*@
                </th>               
           </tr>
            for (var i = 0; i < Model.Products.Count; i++)
            {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => Model.Products[i].Product_ID)
                    @Html.HiddenFor(modelItem => Model.Products[i].Product_ID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => Model.Products[i].ProductName)
                    @Html.HiddenFor(modelItem => Model.Products[i].ProductName)
                </td>
            </tr>
            }
        }
    </table>
    <input type="submit" value="submit" />
}