可能重复:
How can I get this ASP.NET MVC SelectList to work?
到底是怎么回事?在DropDownList of MVC3中有什么bug吗?SelectedValue在标记中没有显示为实际选中的。
我在尝试不同的方法,什么都不管用。
public class SessionCategory
{
public int Id { get; set; }
public string Name { get; set; }
}
public static IEnumerable<SessionCategory> Categories
{
get
{
var _dal = new DataLayer();
return _dal.GetSesionCategories();
}
}
@{
var cats = Infrastructure.ViewModels.Session.Categories;
var sl = new SelectList(cats, "Id", "Name",2);
}
@Html.DropDownList("categories", sl);发布于 2011-04-05 15:38:58
我认为您需要将选定的值设置为字符串。使用详细的here等扩展方法也有一定的价值。
发布于 2011-04-05 15:37:53
尝试以下几点:
型号:
public class MyViewModel
{
public int CategoryId { get; set; }
public IEnumerable<SelectListItem> Categories { get; set; }
}主计长:
public ActionResult Foo()
{
var cats = _dal.GetSesionCategories();
var model = new MyViewModel
{
// Preselect the category with id 2
CategoryId = 2,
// Ensure that cats has an item with id = 2
Categories = cats.Select(c => new SelectListItem
{
Value = c.Id.ToString(),
Text = c.Name
})
};
}查看:
@Html.DropDownListFor(
x => x.CategoryId,
new SelectList(Model.Categories, "Value", "Text")
)https://stackoverflow.com/questions/5554421
复制相似问题