Rob*_*bin 3 c# html.dropdownlistfor asp.net-mvc-2
美好的一天,
我有Html.DropDownListFor这个问题我似乎无法解决..它给了我正确的选项数,如果我在代码中执行断点,它应该呈现列表,"SelectItemList"对象包含具有正确值的项目,但它吐出的实际HTML如下:
<select id="Reason_ReasonId" name="Reason.ReasonId"><option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
</select>
Run Code Online (Sandbox Code Playgroud)
该模块包含:
public SelectList ReasonList
{
get
{
SelectList selectList;
List<SelectListItem> selectItems;
using (var db = new EscalationDataContext())
{
var reasons =
db.Reasons
.OrderBy(r => r.Reason1)
.Select(r => new SelectListItem
{
Value = r.ReasonId.ToString(),
Text = r.Reason1
});
selectItems = reasons.ToList();
selectList = new SelectList(selectItems);
}
return selectList;
}
}
Run Code Online (Sandbox Code Playgroud)
控制器只是创建一个默认实例,并设置默认值:
public ActionResult Create()
{
EscalationModel model = new EscalationModel
{
Reason = new Reason { ReasonId = new Guid("598c28c2-877a-44fa-9834-3241c5ee9355"), Reason1 = "Taken too long" },
ActionedDate = DateTime.Now
};
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
最后但同样重要的是,观点:
<%: Html.DropDownListFor(m => m.Reason.ReasonId, Model.ReasonList) %>
Run Code Online (Sandbox Code Playgroud)
任何想法为什么它的行为像这样?正如我所说的,在实际代码中(在视图中)我有正确的值,但是..它不喜欢我..任何帮助都将非常感谢,提前感谢!
好的..似乎你必须指定SelectListItem中的哪个变量用于"Value"并且用于"Text"..
selectList = new SelectList(selectItems);
Run Code Online (Sandbox Code Playgroud)
成为..
selectList = new SelectList(selectItems, "Value", "Text");
Run Code Online (Sandbox Code Playgroud)
这似乎已经成功了!
| 归档时间: |
|
| 查看次数: |
1061 次 |
| 最近记录: |