How to get the Selected Year and month from the drop down list from view to controller. When i tried its giving error as There is no ViewData item of type 'IEnumerable' that has the key "Yearitems". As i'm a new to Asp.net mvc so any help will be Appriciated. Thanks in Advance.
This is My View
<h3>Search by PhoneNumber:@Html.TextBox("SearchString",ViewBag.CurrentFilter as string) </h3>
<p><h3>Year:@Html.DropDownList("Yearitems", (IEnumerable<SelectListItem>)ViewBag.SelectList as SelectList, "Select Year")</h3>
<h3>Month:@Html.DropDownList("MonthItems",(IEnumerable<SelectListItem>)ViewBag.SelectMonthList as SelectList,"Select Month")</h3></p>
<p><input type="submit" value="Search" /></p>
<script>
$(document).ready(function () {
$("#Yearitems").change(function () {
//alert($("#Yearitems>option:selected").attr("Value"));
$.ajax({
type: "Post",
url: '@Url.Action("GetMonths","AirtelManagement")',
data: { YearId: $("#Yearitems>option:selected").attr("Value") },
datatype: "Json",
success: function (data) {
$("#MonthItems").html("");
$.each(data, function (index, item) {
$("#MonthItems").append(new Option(item.MonthName, item.MonthSelectedId));
});
},
error: function () {
alert("Select Year");
}
});
});
});
</script>
}
This is My controller.
public ActionResult ViewDataOfDatabase(string sortorder, string currentFilter, string searchString, int? page,string SelecetedYear,string SelectedMonth)
{
AirtelManagementModel _Airtelmodel = new AirtelManagementModel();
IEnumerable<clsYearOfDate> SelectList = GetYears();
//IEnumerable<MonthListClass> SelectMonthList = GetMonths(YearId);
IEnumerable<SelectListItem> Yearitems = (from v in SelectList
select new SelectListItem()
{
Value = v.YearSelectedId.ToString(),
Text = v.YearOfDate.ToString(),
});
ViewBag.SelectList = Yearitems;
//IEnumerable<SelectListItem> MonthItems = (from m in SelectMonthList
// select new SelectListItem()
// {
// Value = m.MonthSelectedId.ToString(),
// Text = m.MonthName,
// });
//ViewBag.SelectMonthList = MonthItems;
IEnumerable<SelectListItem> MonthItems = Enumerable.Empty<SelectListItem>();
ViewBag.SelectMonthList = MonthItems;
List<AirtelManagementModel> list = ViewDetails();
ViewBag.CurrentSort = sortorder;
ViewBag.PhoneSortParm = String.IsNullOrEmpty(sortorder) ? "Phone_desc" : "";
if (searchString != null )
{
page = 1;
}
else
{
searchString = currentFilter;
}
//if(searchString!=null)
//{
ViewBag.SelectList = SelecetedYear;
ViewBag.SelectMonthList = SelectedMonth;
ViewBag.CurrentFilter = searchString;
var airteldetails = from _model in list
select _model;
if(!String.IsNullOrEmpty(searchString))
{
airteldetails=airteldetails.Where(A=>A.AirtelNumber.ToString().Contains(searchString.ToString()));
}
//airteldetails=airteldetails.OrderByDescending(A=>A.AirtelNumber);
int pageSize = 5;
int pageNumber = (page ?? 1);
//return View(airteldetails.ToList());
return View(airteldetails.ToPagedList(pageNumber, pageSize));
//}
//if (list.Count > 0)
//{
// var airteldetails = from _model in list
// select _model;
// return View(airteldetails.ToPagedList(pageNumber,pageSize));
//}
//else
//{
// ModelState.AddModelError("Error", "No Data found in Database");
// return RedirectToAction("ImportExcelFile", "AirtelManagement");
//}
}
The name of your Select/DDL needs to be the same as the parameter on your controller action method, ie public ActionResult ViewDataOfDatabase(...,String YearItems, String MonthItems)
or change the name in your @Html.DropDownList("name"...
to SelectedMonth
and SelectedYear
to match that of the DDL