我有以下课程:
public class Note : TableServiceEntity
{
public string Description { get; set; }
public string NoteDetailsJSON { get; set; }
}
它包含我想在视图中放入选择列表的简短描述。
我从表中得到这样的数据。
Notes = noteTable.GetAll()
我的viewmodel看起来像这样:
public IEnumerable<Note> Notes { get; set; }
但是当我尝试填充我的选择框时,我只得到以下内容:
@Html.DropDownListFor(
x => x.Level,
new SelectList(Model.Notes, "Description", "Description"),
new { style = "display: inline;" }
)
<select id="Level" name="Level" style="display: inline;"><option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>
如何填充选择框的一些帮助将非常感激。
答案 0 :(得分:0)
我不知道你的问题是什么。最有可能noteTable.GetAll()
只返回空对象。
假设您有以下视图模型,其中包含注释列表:
public class MyViewModel
{
public string Level { get; set; }
public IEnumerable<Note> Notes { get; set; }
}
并且您的控制器操作正确填充此模型:
public ActionResult Index()
{
var model = new MyViewModel
{
Notes = noteTable.GetAll().ToList() // make sure that this returns some data
};
return View(model);
}
显然,确保您的问题不在数据源中的最佳方法是最初对某些数据进行硬编码:
public ActionResult Index()
{
var model = new MyViewModel
{
Notes = Enumerable.Range(1, 5).Select(x => new Note
{
Description = "note description " + x
})
};
return View(model);
}
在您的视图中,您应该能够显示下拉列表:
@model MyViewModel
@Html.DropDownListFor(
x => x.Level,
new SelectList(Model.Notes, "Description", "Description"),
new { style = "display: inline;" }
)