我有一个 Kendo DropDownList:
@(Html.Kendo().DropDownList()
.Name("concessions")
.HtmlAttributes(new { style = "width:320px" })
.DataTextField("Description")
.DataValueField("Id")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("getConcessionsDDL", "MasterData");
});
})
)
数据源是从 Controller 中的 getConcessionsDDL
方法读取的:
public ActionResult GetConcessionsDDL()
{
ConcessionModel cm = new ConcessionModel();
var aux = cm.getConcessions();
return Json(aux.concessions.Select(sme => new ConcessionModel { Description = sme.concession.mediumDescription, Id = sme.concession.id }).Distinct(), JsonRequestBehavior.AllowGet);
}
它工作正常,DropDownList 已按预期填充。但现在我想定义它的默认选择值。现在,默认值始终是列表的第一个。
我使用了 .SelectedIndex(4)
并且它有效:在本例中,所选索引将为 4。但我想在 Controller 中定义它。
我尝试在 ViewBag
中传递选定的索引,但没有成功:
ViewBag.selectedIndex = 21;
在 View 中:
@(Html.Kendo().DropDownList()
.Name("concessions")
.HtmlAttributes(new { style = "width:320px" })
.DataTextField("Description")
.DataValueField("Id")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("getConcessionsDDL", "MasterData");
});
})
.SelectedIndex(ViewBag.selectedIndex)
)
到目前为止还没有运气。有什么帮助吗?
最佳答案
解决方案很简单,将值添加为 HTML 属性即可正常工作:
.HtmlAttributes(new { value = ViewBag.ConcessionId })
像这样:
@(Html.Kendo().DropDownList()
.Name("concessions")
.HtmlAttributes(new { style = "width:320px" })
.DataTextField("Description")
.HtmlAttributes(new { value = ViewBag.ConcessionId })
.DataValueField("Id")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("getConcessionsDDL", "MasterData");
});
})
)
关于asp.net-mvc - Kendo DropDownList 默认选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25267897/