暫時先記錄一下,後續再整理
View
@Html.DropDownListFor(m => m.CustomType, (SelectList)ViewBag.CustomTypeList, "==請選擇==")
Controller
ViewBag.CustomTypeList = sf.CreatMasterCodeDropDownList("CustomType");
Class
#region CreatMasterCodeDropDownList()-產生下拉選單
public SelectList CreatMasterCodeDropDownList(string columnName)
{
HotelSelectModels hsm = new HotelSelectModels();
SelectList DDLItem = new SelectList(hsm.GetMasterCodeByColumnName(columnName), "Value", "Name");
return DDLItem;
}
#endregion
Model
public List<HotelViewModel.MasterCodeModel> GetMasterCodeByColumnName(string columnName)
{
List<HotelViewModel.MasterCodeModel> result = new List<HotelViewModel.MasterCodeModel>();
using (HotelEntities db = new HotelEntities())
{
var query = db.MasterCode.Where(f => f.ColumnName == columnName).ToList();
if (query.Count > 0)
{
foreach (var item in query)
{
result.Add(new HotelViewModel.MasterCodeModel()
{
ColumnName = item.ColumnName,
Name = item.Name,
Value = item.Value
});
}
}
}
return result;
}
沒有留言:
張貼留言