ASP.NET mvc 的月份和年份下拉列表 [英] Month and Year drop down list for ASP.NET mvc

查看:92
本文介绍了ASP.NET mvc 的月份和年份下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to populate Month and Year in asp.net mvc project's cshtml view page

这是查看页面代码

@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)
            });
        }
    }

}

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

but here I'm getting following error in Model Class

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

The name 'Model' does not exist in the current context

在视图页面中也出现此错误

also getting this error in view page

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

The name 'ExpirationMonthDropdown' does not exist in the current context

推荐答案

使用以下代码更改模型

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" />
}

这篇关于ASP.NET mvc 的月份和年份下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆