如何在网格列的下拉编辑器中显示默认值
这是我的网格:
var grid = Html.Kendo().Grid<UserVM>()
.Name("Grid")
.DataSource(dataSource => dataSource
.Ajax()
.Events(events => events.Error("gridError"))
.PageSize(10)
.ServerOperation(false)
.Model(model =>
{
model.Id(e => e.Id);
model.Field(e => e.Role).DefaultValue((RoleVM)ViewData["DefaultRole"]);
})
.Read("Read", "GeneralInformation")
.Create("Read", "GeneralInformation")
.Update("Update", "GeneralInformation")
.Destroy("Delete", "GeneralInformation")
)
.Columns(columns =>
{
columns.Bound(e => e.FullName).Title(_userLocalizer["Full name"]);
columns.Bound(e => e.Role.Name).Title(_userLocalizer["Role"]).EditorTemplateName("RoleEditor");
columns.Bound(e => e.Position).Title(_userLocalizer["Position"]);
columns.Bound(e => e.PhoneNumber).Title(_userLocalizer["Phone number"]);
columns.Bound(e => e.Email).Title(_userLocalizer["Email"]);
})
.Editable(GridEditMode.InLine)
.Sortable()
.Pageable(pager => pager
.Input(true)
.PageSizes(true)
)
.Scrollable(scroll => scroll
.Virtual(GridVirtualizationMode.RowsAndColumns)
);
我使用编辑器来选择角色。如您所见,我尝试在 dataSource
模型
上指定默认值,但它行不通。
colevm
:
public class RoleVM
{
public string Name { get; set; }
public string Value { get; set; }
public RoleVM(string name, string value)
{
Name = name;
Value = value;
}
}
coleditor
:
@(Html.Kendo().DropDownList()
.Name("Employee")
.DataTextField("Name")
.DataValueField("Value")
.BindTo((IEnumerable<RoleVM>) ViewData["Roles"])
)
uservm
:
public class UserVM
{
public int? Id { get; set; }
public List<ApplicationVM> Applications { get; set; }
[Required(ErrorMessage = "Full name required")]
public string FullName { get; set; }
[Required(ErrorMessage = "Role required")]
public RoleVM Role { get; set; }
[Required(ErrorMessage = "Position required")]
public string Position { get; set; }
[Required(ErrorMessage = "Phone number required")]
[Phone]
public string PhoneNumber { get; set; }
[Required(ErrorMessage = "Email required")]
[EmailAddress(ErrorMessage = "Email invalid")]
public string Email { get; set; }
}
控制器的操作填充数据:
public IActionResult Index()
{
var roles = Static.Roles.Select(role => new RoleVM(_staticLocalizer[role].Value, role));
ViewData["Roles"] = roles;
ViewData["DefaultRole"] = roles.Last();
return View();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将列绑定到角色对象而不是其名称属性,并添加clienttemplate显示名称:
然后更新coleditor:
如果您需要保持开箱即用的过滤器工作,则也可以使用 mvvm 配置绑定。按照您已经定义了列配置:
通过数据绑定属性将编辑器模板绑定到整个对象:
You can bind the column to the Role object rather than it's Name property and add a ClientTemplate to display the Name:
Then update the RoleEditor:
If you need to keep the out-of-the-box filter working you can also use MVVM to configure the binding. Keep the column configuration as you already have it defined:
and bind the editor template to the whole object via the data-bind attribute:
补充@aleksandar答案。添加过滤的正确方法是:
并且列需要将其限制到
roun
:此,
data-Bind
属性是没有用的。Addition to the @Aleksandar answer. The correct way to add filtering is:
And column need to be bounded to
Role
:And with this,
data-bind
attribute is useless.