如何从视图到控制器的下拉列表中获取所选年份和月份。当我尝试给出错误时,因为没有类型' IEnumerable'有关键" Yearitems"。因为我是Asp.net mvc的新手,所以任何帮助都将是Appriciated。在此先感谢。
这是我的观点
<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>
}
这是我的控制器。
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");
//}
}
答案 0 :(得分:1)
Select / DDL的名称必须与控制器操作方法中的参数相同,即public ActionResult ViewDataOfDatabase(...,String YearItems, String MonthItems)
或将@Html.DropDownList("name"...
中的名称更改为SelectedMonth
和{{ 1}}以匹配DDL的
答案 1 :(得分:0)
从FormCollection
:
public ActionResult ViewDataOfDatabase(FormCollection form)
{
string Month = form["MonthItems"].ToString();
.............................
..............................
}