从下拉列表中的月份和年份 ASP.NET 设计 MVC

我正在努力填写观看页面的月份和年份 cshtml 项目 asp.net mvc

这是一个观看页面代码


@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 =&gt;

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 =&gt;

new SelectListItem//
{
Text = x.ToString//,
Value = x.ToString//,
Selected = /x == Model.ExpirationYear/
}/;
}
}

}


但在这里,我在类类中获得以下错误

姓名 'Model' 在当前的上下文中不存在

在View页面上也会收到此错误

姓名 'ExpirationMonthDropdown' 在当前的上下文中不存在
</selectlistitem></selectlistitem></project.models.productstatistics>
已邀请:

二哥

赞同来自:

使用以下代码更改模型。


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 =&gt;
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 =&gt;

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 =&gt; model.SelectedMonth, /IEnumerable<selectlistitem>/ViewBag.Months, "Month"/
@Html.DropDownListFor/model =&gt; model.SelectedYear, /IEnumerable<selectlistitem>/ViewBag.Years, "Year"/

<table class="table">
@if /Model.Products != null &amp;&amp; Model.Products.Count &gt; 0/
{

<tr>
<th>
@Html.DisplayNameFor/modelItem =&gt; Model.Products[0].Product_ID/ @*This approch is wrong , should read from resource file*@
</th>
<th>
@Html.DisplayNameFor/modelItem =&gt; Model.Products[0].ProductName/ @*This approch is wrong , should read from resource file*@
</th>
</tr>

for /var i = 0; i &lt; Model.Products.Count; i++/
{
<tr>
<td>
@Html.DisplayFor/modelItem =&gt; Model.Products[i].Product_ID/
@Html.HiddenFor/modelItem =&gt; Model.Products[i].Product_ID/
</td>
<td>
@Html.DisplayFor/modelItem =&gt; Model.Products[i].ProductName/
@Html.HiddenFor/modelItem =&gt; Model.Products[i].ProductName/
</td>
</tr>
}
}
</table>
<input type="submit" value="submit"/>
}


</selectlistitem></selectlistitem></productstatistics></productstatistics>

要回复问题请先登录注册