【问题标题】:Month and Year drop down list for ASP.NET mvcASP.NET mvc 的月份和年份下拉列表
【发布时间】:2016-02-25 09:44:20
【问题描述】:

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

这是查看页面代码

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

}

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

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

在查看页面中也出现此错误

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

【问题讨论】:

  • 这段代码有很多问题。第一个错误很明显:您在ProductStatistics 方法中访问Model,但那里不存在。 Model 仅作为成员变量存在于视图中。第二个错误是因为你的视图模型是IEnumerable&lt;albaraka.Models.ProductStatistics&gt;,所以即使你使用Model.ExpirationMonthDropdown,它也不起作用,因为enumerable != 1。请显示一些更相关的代码。
  • @CodeCaster 我需要将ExpirationMonthDropdownExpirationYearDropdown 方法移动到控制器方法
  • 不,您需要显示更多代码,例如定义ExpirationMonthExpirationYear 的位置。
  • 第一个错误是由您的行 Selected = (x == Model.ExpirationMonth) (x2) 引发的,因为您的模型不包含属性名称 Model。但是由于您绑定到属性名称ExpirationMonth,所以无论如何它都会被忽略,所以只需删除它。
  • 第二个错误是因为您没有在名为ExpirationMonthDropdownExpirationYearDropdown 的视图中定义IEnumerable&lt;SelectListItem&gt; 属性 - 但是您在视图中使用@model IEnumerable&lt;project.Models.ProductStatistics&gt; 没有任何意义,因此无法理解你认为你想做什么

标签: c# asp.net-mvc linq asp.net-mvc-4 razor


【解决方案1】:

使用以下代码更改您的模型

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

【讨论】:

  • 然后我收到以下错误'IEnumerable&lt;ProductStatistics&gt;' does not contain a definition for 'ExpirationMonthDropdown' and no extension method 'ExpirationMonthDropdown' accepting a first argument of type 'IEnumerable&lt;ProductStatistics&gt;'
  • @kez 你的模型应该是ProductStatistics 而不是'IEnumerable&lt;ProductStatistics 。在我的回答中查看模型
  • 请原谅我在这个模型中提到这个我也有表格,作为一个简单的问题发布,就像这样留下代码 sn-p,一旦我向@model project.Models.ProductStatistics 发送以下错误foreach statement cannot operate on variables of type 'project.Models.ProductStatistics' because 'project.Models.ProductStatistics' does not contain a public definition for 'GetEnumerator'
  • 好的,在这种情况下,我们需要使用ajaxviewbag 填充组合框
  • 我听不懂你说的,我能举个例子吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
  • 2010-10-23
  • 2021-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多