0
我想創建一個部分視圖來創建每個視圖的下拉列表。爲此創建了一個視圖模型和部分視圖。它的創建正確下拉,但沒有顯示選定的值。@ Html.DropDownList不顯示選定的值
下面是查看模型類
public class drowDownVM
{
public string id { get; set; }
public string name { get; set; }
public string cssClass { get; set; }
public string keyColumnName { get; set; }
public string valueColumnName { get; set; }
public string selectedValue { get; set; }
public string viewbagName { get; set; }
public bool isMultipleSelect { get; set; }
public List<int> multipleselectedValue { get; set; }
}
下面是結合下拉
@if (Model != null)
{
if (ViewData != null && ViewData.Count > 0 && ViewData.Keys.Contains(Model.viewbagName))
{
string ddName = !(string.IsNullOrEmpty(Model.name)) ? Model.name : "default-name";
string viewBagName = !(string.IsNullOrEmpty(Model.viewbagName)) ? Model.viewbagName : ViewData.Keys.First();
string keyColumnName = !(string.IsNullOrEmpty(Model.keyColumnName)) ? Model.keyColumnName : "id";
string valueColumnName = !(string.IsNullOrEmpty(Model.valueColumnName)) ? Model.valueColumnName : "id";
string selectedVal = !(string.IsNullOrEmpty(Model.selectedValue)) ? Model.selectedValue : "";
List<int> multipleSelectVal = (Model.multipleselectedValue != null && Model.multipleselectedValue.Count > 0) ? Model.multipleselectedValue : new List<int>();
var cssClass = !(string.IsNullOrEmpty(Model.cssClass)) ? Model.cssClass : "";
if (!Model.isMultipleSelect)
{
<div>
@Html.DropDownList(ddName, new SelectList((System.Collections.IEnumerable)ViewData[viewBagName], keyColumnName, valueColumnName, 2), "--Select--", new { @class = cssClass, @data_Val = selectedVal })
</div>
}
else
{
@Html.ListBox(ddName, new MultiSelectList((System.Collections.IEnumerable)ViewData[viewBagName], keyColumnName, valueColumnName, multipleSelectVal), new { @class = cssClass, @multiple = "multiple"})
}
}
}
else
{
<p class="hidden">Model Data Not Found!</p>
}
下面是其中要求局部視圖綁定下拉碼,第一個是單個選擇局部視圖,第二個是呼叫多個選擇。
@Html.Partial("_dropdown", new drowDownVM() { cssClass = "form-control", id = "TargetTypeList", keyColumnName = "code_val", name = "TargetTypeList", selectedValue = "1", valueColumnName = "code_name", viewbagName = "TargetTypeList"})
@Html.Partial("_dropdown", new drowDownVM() { cssClass = "form-control", id = "TargetTypeList", isMultipleSelect = true, keyColumnName = "code_val", name = "TargetTypeList", selectedValue = "1", valueColumnName = "code_name", viewbagName = "TargetTypeList", multipleselectedValue = new List<int>() { 1, 2 } })
注:如果你想創建一個resusable下拉組件纔沒有必要去爲局部的觀點,你可以嘗試在EditorTemplate MVC –
下拉列表中沒有顯示所選值。這是什麼意思 ?你沒有得到控制器中的選定值?或者當用戶嘗試在ui中選擇下拉值時,它並未被選中?請告訴我們真正的問題。問題有點混淆 –
假設在編輯模式下使用這個局部視圖並且想顯示默認的選定值,那時它不顯示選定的值。 – DaxeshVora