2017-01-16 62 views
-1

我有一个Quantity下拉列表中填充了来自sql的值。如何获取从数据库填充的下拉列表的选定值?

查看

@Html.DropDownList("ShirtQuantityId", null, htmlAttributes: new { @class = "form-control" }) 

控制器

ViewBag.ShirtQuantityId = new SelectList(db.ShirtQuantities, "Id", "Name"); 

在这种情况下,我怎么会去得到Quantity回到控制器的选择的价值?

+0

帖子的形式与参数(或模型属性)控制器方法叫做:ShirtQuantityId –

+0

@NineBerry是的,它的作用: ) – Skullomania

回答

1

你能帮

@Html.DropDownListFor(x => x.QuantityId , ViewBag.ShirtQuantityId, htmlAttributes: new { @class = "form-control" }) 
+1

您需要将Viewbag中的值转换为正确的类型:'(SelectList)ViewBag.ShirtQuantityId' – NineBerry

0
List<SelectListItem> items= new List<SelectListItem>(); 
db.ShirtQuantities.Each(a=>{ 
    items.Add(new SelectListItem{ 
     Text=a.Id, 
     Value=a.Name 
    }); 

} 

ViewBag.ShirtQuantityId = items 

@Html.DropDownList("ShirtQuantityId", null, htmlAttributes: new { @class = "form-control" }) 
相关问题