我想在dropdownbox的视图中使用以下属性。 我怎么能 ? 我尝试了很多方法但是 请帮忙
public IEnumerable<SelectListItem> Topic
{
get
{
return new[]
{
new SelectListItem { Value = "1", Text = "Science", Selected=true },
new SelectListItem { Value = "2", Text = "History" },
new SelectListItem { Value = "3", Text = "Physics" },
};
}
}
答案 0 :(得分:0)
您尚未添加观看代码。
基本上您需要在视图中添加@model
,如下所示:
@model MySampleApplication.Models.ModelName
然后在你看来你应该像这样访问:
@Html.DropdownListFor(m=>m.Topic, Model.Topic)
来自Scott Allen的DropDownListFor with ASP.NET MVC
希望有所帮助
修改强>
假设你有这样的模型类:
public class MyModel
{
public IEnumerable<SelectListItem> Topic
{
get
{
return new[]
{
new SelectListItem { Value = "1", Text = "Science", Selected=true },
new SelectListItem { Value = "2", Text = "History" },
new SelectListItem { Value = "3", Text = "Physics" },
};
}
}
}
试试这样:
public ActionResult Index()
{
MyModel model=new MyModel();
return View(model);
}
答案 1 :(得分:0)
试试这个
型号
public SelectList Topic
{
get
{
return new SelectList(
new List<SelectListItem> {
new SelectListItem { Value = "1", Text = "Science", Selected = true },
new SelectListItem { Value = "2", Text = "History" },
new SelectListItem { Value = "3", Text = "Physics" }
},"Value","Text"
);
}
}
public int selectedType {get;set;}
查看强>
@Html.DropDownListFor(model => model.selectedType , Model.Topic)