List<SelectListItem> quantity = new List<SelectListItem>();
quantity.Add(new SelectListItem() { Text = "1", Value = "1" });
quantity.Add(new SelectListItem() { Text = "2", Value = "2" });
quantity.Add(new SelectListItem() { Text = "3", Value = "3" });
quantity.Add(new SelectListItem(){ Text = "4", Value = "4" });
quantity.Add(new SelectListItem() { Text = "5", Value = "5" });
quantity.Add(new SelectListItem() { Text = "6", Value = "6" });
quantity.Add(new SelectListItem() { Text = "7", Value = "7" });
quantity.Add(new SelectListItem() { Text = "8", Value = "8" });
quantity.Add(new SelectListItem() { Text = "9", Value = "9" });
ViewBag.Li = new SelectList(quantity, "Text", "Value");
我已经在我的一个控制器中列出了
它可以查看
@{
ViewBag.Title = "DisplayProducts";
Layout = "~/Views/Shared/_LayoutPage2.cshtml";
}
@using (Html.BeginForm("dummyuser", "SignUp", FormMethod.Post, new { data = Model, List= ViewBag.Li }))
{
<table>
@Html.EditorFor(x => x.Products)
</table>
<p>
我使用了编辑器模板来避免循环条件,因此在编辑器模板视图中保留了dropdownlist 如下所示
<td>@Html.DisplayFor(model => model.Price)</td>
<td>@Html.CheckBoxFor(model => model.checkIn)</td>
@Html.HiddenFor(model=>model.Id)
<td>@Html.DropDownList("Li",ViewBag.Li as SelectList, string.Empty)</td>
我能够获取列表,但无法将选定的列表项发回给控制器,如下所示
[HttpPost]
public ActionResult dummyuser(Product data,List<SelectListItem> List)
{
List<ProductDetails> list = data.Products;
// rest of code...
任何人都可以在我出错的地方帮助我。提前致谢