Asp.net mvc 在MVC中使用SelectList中的选定值试图保留值时无效
我试图让我的Html.DropDownList保留一些用户选择的值 代码将使用SelectList中的selected值来保留值,以使dropdownlist保留用户先前选择的值。该页面是在MVC下构建的。在尝试了一段时间后,我发现该网页只能返回其原始状态(在用户选择任何内容之前),但在按下提交按钮后无法保留任何dropdownlist值 我还使用jQuery控制多个下拉列表的显示和隐藏,我只需要显示一个组下拉列表,并在选择值时显示另一个值(零售、公共汽车、自行车、轨道等,…是组下拉列表的值和其他下拉列表的名称) 以下是我的代码: 控制器:Asp.net mvc 在MVC中使用SelectList中的选定值试图保留值时无效,asp.net-mvc,drop-down-menu,Asp.net Mvc,Drop Down Menu,我试图让我的Html.DropDownList保留一些用户选择的值 代码将使用SelectList中的selected值来保留值,以使dropdownlist保留用户先前选择的值。该页面是在MVC下构建的。在尝试了一段时间后,我发现该网页只能返回其原始状态(在用户选择任何内容之前),但在按下提交按钮后无法保留任何dropdownlist值 我还使用jQuery控制多个下拉列表的显示和隐藏,我只需要显示一个组下拉列表,并在选择值时显示另一个值(零售、公共汽车、自行车、轨道等,…是组下拉列表的值和其
public List<SelectListItem> SetGroup(string group)
{
List<SelectListItem> groups = new List<SelectListItem>();
groups.AddRange(new SelectList(GMManager.FindAllGroup(), "MerchantNo", "MerchantName", group));
groups.Add(new SelectListItem() { Text = "ALL", Value = "ALL", Selected = false });
return groups;
}
public List<SelectListItem> SetRetail(string retail)
{
List<SelectListItem> retails = new List<SelectListItem>();
retails.AddRange(new SelectList(GMManager.FindAllRetail(), "MerchantNo", "MerchantName",retail));
retails.Add(new SelectListItem() { Text = "ALL", Value = "ALL", Selected = false });
return retails;
}
public List<SelectListItem> SetBus(string bus)
{
List<SelectListItem> buses = new List<SelectListItem>();
buses.AddRange(new SelectList(GMManager.FindAllBus(), "MerchantNo", "MerchantName",bus));
buses.Add(new SelectListItem() { Text = "ALL", Value = "ALL", Selected = false });
return buses;
}
public List<SelectListItem> SetBike(string bike)
{
List<SelectListItem> bikes = new List<SelectListItem>();
bikes.AddRange(new SelectList(GMManager.FindAllBike(), "MerchantNo", "MerchantName",bike));
bikes.Add(new SelectListItem() { Text = "ALL", Value = "ALL", Selected = false });
return bikes;
}
public List<SelectListItem> SetTrack(string track)
{
List<SelectListItem> tracks = new List<SelectListItem>();
tracks.AddRange(new SelectList(GMManager.FindAllTrack(), "MerchantNo", "MerchantName",track));
tracks.Add(new SelectListItem() { Text = "ALL", Value = "ALL", Selected = false });
return tracks;
}
public List<SelectListItem> SetParking(string parking)
{
List<SelectListItem> parkings = new List<SelectListItem>();
parkings.AddRange(new SelectList(GMManager.FindAllParking(), "MerchantNo", "MerchantName",parking));
parkings.Add(new SelectListItem() { Text = "ALL", Value = "ALL", Selected = false });
return parkings;
}
public List<SelectListItem> SetOutsourcing(string outsourcing)
{
List<SelectListItem> outsourcings = new List<SelectListItem>();
outsourcings.AddRange(new SelectList(GMManager.FindAllOutsourcing(), "MerchantNo", "MerchantName",outsourcing));
outsourcings.Add(new SelectListItem() { Text = "ALL", Value = "ALL", Selected = false });
return outsourcings;
}
public ActionResult RPT_160501()
{
string start = DateTime.Now.ToString("yyyyMMdd");
string end = DateTime.Now.ToString("yyyyMMdd");
string group = "";
string retail = "";
string bus = "";
string bike = "";
string track = "";
string parking = "";
string outsourcing = "";
DataTable dt = null;
if (Request.Form["searchConfirm"] != null || Request.Form["ExportExcel"] != null)
{
start = Request.Form["startDate"];
end = Request.Form["endDate"];
group = Request.Form["group"];
retail = Request.Form["retail"];
bus = Request.Form["bus"];
bike = Request.Form["bike"];
track = Request.Form["track"];
parking = Request.Form["parking"];
outsourcing = Request.Form["outsourcing"];
}
ViewBag.Group = SetGroup(group);
ViewBag.Retail = SetRetail(retail);
ViewBag.Bus = SetBus(bus);
ViewBag.Bike = SetBike(bike);
ViewBag.Track = SetTrack(track);
ViewBag.Parking = SetParking(parking);
ViewBag.Outsourcing = SetOutsourcing(outsourcing);
ViewBag.RepName = "ReportName";
ViewBag.Start = start;
ViewBag.End = end;
公共列表集合组(字符串组)
{
列表组=新列表();
AddRange(新的SelectList(GMManager.FindAllGroup(),“MerchantNo”,“MerchantName”,group));
添加(新建SelectListItem(){Text=“ALL”,Value=“ALL”,Selected=false});
返回组;
}
公共列表集合零售(字符串零售)
{
列表零售=新列表();
retails.AddRange(新的SelectList(GMManager.FindAllRetail(),“MerchantNo”,“MerchantName”,retail));
retails.Add(new SelectListItem(){Text=“ALL”,Value=“ALL”,Selected=false});
退货零售;
}
公共列表立根总线(字符串总线)
{
列表总线=新列表();
AddRange(新的选择列表(GMManager.FindAllBus(),“MerchantNo”,“MerchantName”,bus));
添加(新SelectListItem(){Text=“ALL”,Value=“ALL”,Selected=false});
回程巴士;
}
公共列表(字符串自行车)
{
列表=新列表();
AddRange(新的选择列表(GMManager.FindAllBike(),“MerchantNo”,“MerchantName”,bike));
添加(新建SelectListItem(){Text=“ALL”,Value=“ALL”,Selected=false});
归还自行车;
}
公共列表设置轨道(字符串轨道)
{
列表轨道=新列表();
tracks.AddRange(新的选择列表(GMManager.FindAllTrack(),“MerchantNo”,“MerchantName”,track));
添加(新SelectListItem(){Text=“ALL”,Value=“ALL”,Selected=false});
返回轨道;
}
公共列表设置停车(字符串停车)
{
列表停车=新列表();
AddRange(新的选择列表(GMManager.FindAllParking(),“MerchantNo”,“MerchantName”,parking));
添加(新的SelectListItem(){Text=“ALL”,Value=“ALL”,Selected=false});
返回停车场;
}
公共列表集合外包(字符串外包)
{
列表外包=新列表();
AddRange(新的选择列表(GMManager.FindAllOutsourcing(),“MerchantNo”,“MerchantName”,outsourcing));
Add(new SelectListItem(){Text=“ALL”,Value=“ALL”,Selected=false});
返回外包;
}
公共行动结果RPT_160501()
{
字符串start=DateTime.Now.ToString(“yyyyMMdd”);
字符串end=DateTime.Now.ToString(“yyyyMMdd”);
字符串组=”;
字符串retail=“”;
字符串总线=”;
字符串bike=“”;
字符串轨道=”;
字符串=”;
字符串=”;
数据表dt=null;
if(Request.Form[“searchConfirm”]!=null | | Request.Form[“ExportExcel”]!=null)
{
开始=请求。表单[“开始日期”];
结束=请求。表格[“结束日期”];
组=请求。表格[“组”];
零售=申请表[“零售”];
总线=请求。表格[“总线”];
自行车=申请表[“自行车”];
跟踪=请求。表格[“跟踪”];
停车=请求。表格[“停车”];
外包=申请表[“外包”];
}
ViewBag.Group=SetGroup(组);
ViewBag.Retail=SetRetail(零售);
ViewBag.Bus=SetBus(总线);
ViewBag.Bike=SetBike(Bike);
ViewBag.Track=SetTrack(轨道);
ViewBag.Parking=设置停车(停车);
ViewBag.Outsourcing=设置外包(Outsourcing);
ViewBag.RepName=“ReportName”;
ViewBag.Start=开始;
ViewBag.End=End;
视图:
$(文档).ready(函数(){
$(“#零售”).hide();
$(“#总线”).hide();
$(“#自行车”).hide();
$(“#跟踪”).hide();
$(“#停车”).hide();
$(“#外包”).hide();
$(“#组”)。关于(“更改”,函数(){
开关($(this.val()){
案例“零售”:
$(“#零售”).show();
$(“#总线”).hide();
$(“#自行车”).hide();
$(“#跟踪”).hide();
$(“#停车”).hide();
$(“#外包”).hide();
打破
案例“巴士”:
$(“#零售”).hide();
$(“#总线”).show();
$(“#自行车”).hide();
$(“#跟踪”).hide();
$(“#停车”).hide();
$(“#外包”).hide();
打破
案例“自行车”:
$(“#零售”).hide();
$(“#总线”).hide();
$(“#自行车”).show();
$(“#跟踪”).hide();
$(“#停车”).hide();
$(“#外包”).hide();
<script>
$(document).ready(function () {
$("#retail").hide();
$("#bus").hide();
$("#bike").hide();
$("#track").hide();
$("#parking").hide();
$("#outsourcing").hide();
$("#group").on("change", function () {
switch ($(this).val()) {
case "RETAIL":
$("#retail").show();
$("#bus").hide();
$("#bike").hide();
$("#track").hide();
$("#parking").hide();
$("#outsourcing").hide();
break;
case "BUS":
$("#retail").hide();
$("#bus").show();
$("#bike").hide();
$("#track").hide();
$("#parking").hide();
$("#outsourcing").hide();
break;
case "BIKE":
$("#retail").hide();
$("#bus").hide();
$("#bike").show();
$("#track").hide();
$("#parking").hide();
$("#outsourcing").hide();
break;
case "TRACK":
$("#retail").hide();
$("#bus").hide();
$("#bike").hide();
$("#track").show();
$("#parking").hide();
$("#outsourcing").hide();
break;
case "PARKING_LOT":
$("#retail").hide();
$("#bus").hide();
$("#bike").hide();
$("#track").hide();
$("#parking").show();
$("#outsourcing").hide();
break;
case "BANK_OUTSOURCING":
$("#retail").hide();
$("#bus").hide();
$("#bike").hide();
$("#track").hide();
$("#parking").hide();
$("#outsourcing").show();
break;
default:
$("#retail").hide();
$("#bus").hide();
$("#bike").hide();
$("#track").hide();
$("#parking").hide();
$("#outsourcing").hide();
break;
}
})
})
</script>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.RepName</title>
<h2>@ViewBag.RepName</h2>
@using (Html.BeginForm("RPT_160501", "ReportSystem", FormMethod.Post, new { id = "searchForm" }))
{
<div>
<label for="startDate"> Date:</label>
<input type="text" name="StartDate" id="StartDate" value="@ViewBag.Start" style="width:100px;" /> ~
<input type="text" name="EndDate" id="EndDate" value="@ViewBag.End" style="width:100px;" />
@Html.DropDownList("group", (IEnumerable<SelectListItem>)ViewBag.Group, "--Please Choose--")
@Html.DropDownList("retail", (IEnumerable<SelectListItem>)ViewBag.Retail, "--Please Choose--")
@Html.DropDownList("bus", (IEnumerable<SelectListItem>)ViewBag.Bus, "--Please Choose--")
@Html.DropDownList("bike", (IEnumerable<SelectListItem>)ViewBag.Bike, "--Please Choose--")
@Html.DropDownList("track", (IEnumerable<SelectListItem>)ViewBag.Track, "--Please Choose--")
@Html.DropDownList("parking", (IEnumerable<SelectListItem>)ViewBag.Parking, "--Please Choose--")
@Html.DropDownList("outsourcing", (IEnumerable<SelectListItem>)ViewBag.Outsourcing, "--Plase Choose--")
<input type="submit" name="searchConfirm" value="Search" class="btn btn-default" />
<input type="submit" name="ExportExcel" value="Export Excel" class="btn btn-default" />
</div>
}
<table class="grid" width="100%">
<thead>
<tr style="background-color: lightgray">
<th>
A
</th>
<th>
B
</th>
<th>
C
</th>
<th>
D
</th>
</tr>
</thead>
<tbody>