我有以下行动方法:
public ActionResult AdvanceSearch(string AssetTypeName)
{
ViewBag.Techtypes = repository.GetAllTechnologyType().ToList();
ViewBag.AssetID = repository.GetTechnologyTypeID(AssetTypeName);
return View();
}它将调用folloiwng视图:- //代码在这里
@Html.DropDownListFor(model =>model.AssetTypeID, ((IEnumerable<TMS.Models.TechnologyType>)ViewBag.Techtypes).Select(option => new SelectListItem {
Text = (option == null ? "None" :option.Name),
Value = option.AssetTypeID.ToString(),
Selected = (Model != null) && (option.AssetTypeID == ViewBag.AssetID)
}), "All")//代码在这里
但是下拉列表将始终显示默认值“All”,而不是选择与viewBag值option.AssetTypeID == ViewBag.AssetID匹配的项。明朝表示,ViewBag将有正确的价值。谁能说出什么问题了?谢谢
发布于 2013-11-17 00:56:29
我觉得你的情况不对。尝尝这个。检查“选中”属性。
@Html.DropDownListFor(model => model.AssetTypeID, ((IEnumerable<TechnologyType>)ViewBag.Techtypes).Select(option => new SelectListItem
{
Text = (option == null ? "None" : option.Name),
Value = option.AssetTypeID.ToString(),
Selected = (option.AssetTypeID == (int)ViewBag.AssetID)
}), "All")https://stackoverflow.com/questions/20025692
复制相似问题