【发布时间】:2016-06-09 07:31:02
【问题描述】:
我正在使用 ViewModel 在我的 asp .net MVC 应用程序中创建视图。在这个视图中,我需要绑定一个下拉列表(成功绑定)。 问题:我想在下拉列表中添加具有值的额外项目,例如“select”、“0”
这是我的视图模型:
public class PagesViewModel
{
public int Id { get; set; }
[Required]
[Display(Name = "Page Name")]
public string PageName { get; set; }
[Display(Name = "Parent Page")]
public IEnumerable<Page> Page { get; set; }
}
我的控制器:
public ActionResult Create()
{
var model = new PagesViewModel
{
Page = db.Pages.Where(s => s.IsActive == true).OrderBy(r => r.PageName)
};
//--- Here I need to add extra items manually
return View(model);
}
我的观点:
@Html.DropDownListFor(model => model.Id, new SelectList(Model.Page, "Id", "PageName"), "Select Parent Page", htmlAttributes: new { @class = "form-control" })
【问题讨论】:
标签: asp.net-mvc