如何从下拉列表中获取选中的项
本文关键字:获取 下拉列表 | 更新日期: 2023-09-27 18:07:07
我知道这是一个很多人在网站上回答的问题,但似乎没有解决我的问题的方法。我是MVC的新手,不知道如何将下拉列表中的选定项发送到控制器。
public class MonthDropDownList
{
public IEnumerable<SelectListItem> Months
{
get
{
return DateTimeFormatInfo
.InvariantInfo
.MonthNames
.Where(m => !String.IsNullOrEmpty(m) )
.Select((monthName, index) => new SelectListItem
{
Value = (index + 1).ToString(),
Text = monthName
});
}
}
public int SelectedMonth { get; set; }
}
这是我的观点:
@model Plotting.Models.MonthDropDownList
@Html.DropDownListFor(x => x.SelectedMonth, Model.Months)
@using (Html.BeginForm("MonthlyReports", "Greenhouse", FormMethod.Post))
{
<input type="submit" name="btnSubmit" value="Monthly Report" />
}
下面是ActionResult,我应该在其中使用选定的日期:
public ActionResult MonthlyReports(MonthDropDownList Month)
{
Debug.Write("Month" + Month.SelectedMonth);// <- always = 0
InitChartModel();
cDate.DateTitle = "Day";
string msg = dal.Connection("month");
List<Greenhouse> greenhouse = dal.FindIfDMY("month" , Month.SelectedMonth , msg);
cDate.DateData = GetChart(greenhouse, "month");
return View("MonthlyReports", cDate);
}
你应该把下拉列表移到你的表单中。
@model Plotting.Models.MonthDropDownList
@using (Html.BeginForm("MonthlyReports", "Greenhouse", FormMethod.Post))
{
@Html.DropDownListFor(x => x.SelectedMonth, Model.Months)
<input type="submit" name="btnSubmit" value="Monthly Report" />
}
您的表单控件需要在表单标签
@using (Html.BeginForm("MonthlyReports", "Greenhouse", FormMethod.Post))
{
@Html.DropDownListFor(x => x.SelectedMonth, Model.Months) // move here
<input type="submit" name="btnSubmit" value="Monthly Report" />
}