先獲取List
在Controller里面寫好獲取List,然后通過ViewBag傳到視圖
ViewBag.testList = _testBll.GettestList();
這樣獲取到了List集合
視圖處理
首先,接收一下
@{
var testList = ViewBag.testList;
}
然后,寫一個HTML標簽,使用select標簽,value是id,顯示的是name
<th>測試下拉框</th>
<td data-title="測試下拉框">
<select id="test" name="test" style="width: 150px; height: 23px;">
@foreach (test item in testList)
{
<option selected="selected" value="@item.id">@item.Name</option>
}
</select>
</td>
這樣是可以了,但是當我編輯的時候,我希望下拉框還是顯示出來我以前選擇的內容,視圖部分更新為下
<select id="test" name="test" style="width: 150px; height: 23px;">
@foreach (test item in testList)
{
<option value="@item.id" @if (item.Name==Model.ProductWarehouse.Name) { <text>selected="selected" </text> }>@item.Name</option>
}
</select>
我是取回了我上次存儲的內容,然后判斷一下,如果Name一樣了,就設置為selected。