我正在尝试在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”
答案 0 :(得分:3)
使用以下代码更改您的模型
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" />
}