目标:
当我选择月份的下拉列表时,下拉列表年份和月份中的所选数据将被转移到actionresult索引的参数,而不使用提交按钮。当您在月份的下拉列表中进行选择时,数据将被发送到控制器。
当数据输入到参数并结束到视图()然后我希望刷新网页。
问题:
我不知道该怎么做。
的信息:
*我在VS2013上使用asp.net mvc 4
*请记住,我必须拥有“ViewBag.YearList = Yearlist;”和ViewBag.MonthList = monthlist;“因为它用于生产阶段。
* https://dotnetfiddle.net/Hg9CKn
*您可以说下拉列表的链接类似于@Html.ActionLink("LINK","Index", "Home", new {@year = *here*, @year = *here*})
*当您单击下拉列表一个月时,我希望在显示cshtml中的信息之前从头开始刷新网页索引(如果可能)。
谢谢!
using System;
using System.Web.Mvc;
using System.Collections.Generic;
namespace HelloWorldMvcApp
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index(string year, string month)
{
if (year == null)
{
year = "2000";
}
if (month == null)
{
month = "1";
}
List<SelectListItem> Yearlist = GetYear(year);
List<SelectListItem> monthlist = GetMonth(month);
ViewBag.YearList = Yearlist;
ViewBag.MonthList = monthlist;
return View();
}
private List<SelectListItem> GetYear(string selectedyear)
{
List<SelectListItem> Yearlist = new List<SelectListItem>();
Yearlist.Add(new SelectListItem
{
Selected = "2000" == selectedyear ? true : false,
Text = "2000",
Value = "2000"
});
Yearlist.Add(new SelectListItem
{
Selected = "2001" == selectedyear ? true : false,
Text = "2001",
Value = "2001"
});
return Yearlist;
}
private List<SelectListItem> GetMonth(string selectedmonth)
{
List<SelectListItem> monthlist = new List<SelectListItem>();
monthlist.Add(new SelectListItem
{
Selected = "0" == selectedmonth ? true : false,
Text = "All months",
Value = "0"
});
monthlist.Add(new SelectListItem
{
Selected = "1" == selectedmonth ? true : false,
Text = "Jan",
Value = "1"
});
monthlist.Add(new SelectListItem
{
Selected = "2" == selectedmonth ? true : false,
Text = "Feb",
Value = "2"
});
monthlist.Add(new SelectListItem
{
Selected = "3" == selectedmonth ? true : false,
Text = "Mars",
Value = "3"
});
monthlist.Add(new SelectListItem
{
Selected = "4" == selectedmonth ? true : false,
Text = "April",
Value = "4"
});
monthlist.Add(new SelectListItem
{
Selected = "5" == selectedmonth ? true : false,
Text = "Maj",
Value = "5"
});
monthlist.Add(new SelectListItem
{
Selected = "6" == selectedmonth ? true : false,
Text = "June",
Value = "6"
});
monthlist.Add(new SelectListItem
{
Selected = "7" == selectedmonth ? true : false,
Text = "July",
Value = "7"
});
monthlist.Add(new SelectListItem
{
Selected = "8" == selectedmonth ? true : false,
Text = "August",
Value = "8"
});
return monthlist;
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
</head>
<body>
@Html.DropDownList("Year", (IEnumerable<SelectListItem>)ViewBag.YearList, new { id = "selectedyear" })
@Html.DropDownList("Month", (IEnumerable<SelectListItem>)ViewBag.MonthList, new { id = "selectedmonth" })
</body>
</html>
答案 0 :(得分:0)
您需要将DropDownLists放入表单中。请从Index方法中删除HttpGet属性,并在视图中使用以下代码:
<script src="http://code.jquery.com/jquery-1.8.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#selectedyear').change(function() {
window.frmYearMonth.submit();
});
$('#selectedmonth').change(function() {
window.frmYearMonth.submit();
});
});
</script>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "frmYearMonth" }))
{
ViewContext.Writer.Write(Html.DropDownList("Year", (IEnumerable<SelectListItem>)ViewBag.YearList, new { id = "selectedyear", }));
ViewContext.Writer.Write(Html.DropDownList("Month", (IEnumerable<SelectListItem>)ViewBag.MonthList, new { id = "selectedmonth" }));
}