Reputation: 2313
I'm trying to populate Month and Year in asp.net mvc project's cshtml view page
this is the view page code
@model IEnumerable<project.Models.ProductStatistics>
@{
}
@Html.DropDownList("ExpirationMonth", ExpirationMonthDropdown)
@Html.DropDownList("ExpirationYear", ExpirationYearDropdown)
this is the model
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
The name 'ExpirationMonthDropdown' does not exist in the current context
Upvotes: 0
Views: 15226
Reputation: 1956
Change your model with following code
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;
}
}
In the action
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
}
in your view
@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" />
}
Upvotes: 5