好的,所以我得到了dropDown框,我有我的选项,如果我选择任何一个,我想写下提交的所选项目的价值,听起来很容易吗?好吧,每当我点击任何选项并提交它时,该值都显示在URL上,但我似乎无法将其写出来......
查看(Index.cshtml):
<p>Dropdown Using viewbag with Html.DropDownList </p>
<br/>
<div>
@using (Html.BeginForm("dropDown", "Home", FormMethod.Get))
{
@Html.DropDownList("ListItem")
<button type="submit"></button>
}
</div>
<p>@ViewBag.SelectedValue</p> //should print the value I selected but it does nothing...
Controller(HomeController.cs):
public ActionResult Index()
{
List<SelectListItem> ObjItem = new List<SelectListItem>()
{
new SelectListItem {Text="Select",Value="0",Selected=true },
new SelectListItem {Text="ASP.NET",Value="1" },
new SelectListItem {Text="C#",Value="2"},
new SelectListItem {Text="MVC",Value="3"},
new SelectListItem {Text="SQL",Value="4" },
};
ViewBag.ListItem = ObjItem;
return View("Index");
}
public ActionResult dropDown(List<SelectListItem> ListItem)
{
ViewBag.SelectedValue = ListItem[0].Value; //crossing fingers this has the value I want to print
return this.View("Index");
}
答案 0 :(得分:3)
试试这个
查看(Index.cshtml):
<div>
@using (Html.BeginForm("dropDown", "Home", FormMethod.Post))
{
@Html.DropDownList("ListItem")
<button type="submit">Submit</button>
}
</div>
<p>@ViewBag.SelectedValue</p>
Controller(HomeController.cs):
public ActionResult Index()
{
bindCombo();
return View();
}
private void bindCombo()
{
List<SelectListItem> ObjItem = new List<SelectListItem>()
{
new SelectListItem {Text="Select",Value="Select",Selected=true },
new SelectListItem {Text="ASP.NET",Value="ASP.NET" },
new SelectListItem {Text="C#",Value="C#"},
new SelectListItem {Text="MVC",Value="MVC"},
new SelectListItem {Text="SQL",Value="SQL" },
};
ViewBag.ListItem = ObjItem;
}
[HttpPost]
public ActionResult dropDown(string ListItem)
{
ViewBag.SelectedValue = ListItem;
bindCombo();
return View("index");
}
答案 1 :(得分:0)
就像斯蒂芬·穆克说的那样,你不能发布这份名单。
在index.cshtml文件中更改此行
@Html.DropDownList("ListItem",(List<SelectListItem>)ViewBag.ListItem)
更改控制器中的dropDown方法
public ActionResult dropDown(int ListItem)
{
List<SelectListItem> ObjItem = new List<SelectListItem>()
{
new SelectListItem {Text="Select",Value="0",Selected=true },
new SelectListItem {Text="ASP.NET",Value="1" },
new SelectListItem {Text="C#",Value="2"},
new SelectListItem {Text="MVC",Value="3"},
new SelectListItem {Text="SQL",Value="4" },
};
ViewBag.ListItem = ObjItem;
ViewBag.SelectedValue = ListItem;
return View("Index");
}
答案 2 :(得分:0)
问题是单击“提交”时无法接收SelectListItem。你的行动将收到idx。此代码将执行您想要的操作:
List<SelectListItem> ObjItem = new List<SelectListItem>()
{
new SelectListItem {Text="Select",Value="0",Selected=true },
new SelectListItem {Text="ASP.NET",Value="1" },
new SelectListItem {Text="C#",Value="2"},
new SelectListItem {Text="MVC",Value="3"},
new SelectListItem {Text="SQL",Value="4" },
};
public ActionResult Index()
{
ViewBag.ListItem = ObjItem;
return View("Index");
}
public ActionResult dropDown(string ListItem)
{
ViewBag.ListItem = ObjItem;
ViewBag.SelectedValue = ObjItem.Where(i => i.Value == ListItem).FirstOrDefault().Text;
return this.View("Index");
}