2017-01-16 59 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" }) 
相關問題