2017-01-02 61 views
0

這裏我想在MVC中的HttpPost之後單擊它時,在動態下拉列表中選擇一個屬性「選擇」,它顯示相同的第一個文本「請選擇一個類別」。使用下拉列表中的動態值設置屬性

@if (Model.objlist1.Count != 0) { 
    <select id="catID" onchange="getID();this.form.submit();"> 
     <option id="removeIT" value="007">Please select a category</option> 
     @foreach (var item in Model.objlist1) { 
      //a++; 
      <option value="@item.categoryID">@item.category</option> 
     } 
    </select> 
    @Html.HiddenFor(m => m.categoryID, new { @id = "newCatID", @name = "newCatID" }) 
} 

從HttpPost我想用價值來檢查和選項標籤「中選擇」設置屬性以獲得在動態降「選擇」屬性foreach循環選項標籤內上下重裝後。

回答

0

嘗試這樣 在後端通過類別

ViewData["category"] = new SelectList(
new List<SelectListItem> 
{ 
    new SelectListItem { Text = "Cat1", Value = 1}, 
    new SelectListItem { Text = "Cat2", Value = 2}, 
}, "Value" , "Text"); 

在你看來

@Html.DropDownListFor(model => model.categoryID, ViewData["category"] as IEnumerable<SelectListItem>, "Please select a category") 
+0

但我如何能組屬性中查看頁面「選擇」而不是後端類別,因爲你可以在上面看到我在選項標籤上使用foreach循環,我以這種方式需要它。 –

+0

它將自動選擇取決於值 –

+0

問題已解決將選項標記的值添加到viewbag,然後將其設置在$(「#catID」)。val('@ ViewBag.ID')文檔就緒函數內 –