Blazor的Telerik UI?下载30天免费试用

FormItem模板-自定义编辑器

可以提供自己的自定义编辑器,而不是表单可以生成的默认编辑器.要做到这一点,请使用模板FormItem

当使用模板时,来自表单的内置验证消息将不会被呈现。相反,您可以使用Telerik验证工具来提供验证消息,或任何其他合适的组件,例如ValidationMessage这是框架自带的。

在本文中,你可以找到以下例子:

使用模板提供自定义编辑器

@*使用模板更改默认编辑器*@ @using System.ComponentModel.DataAnnotations                  @code {public TemplateModel MyModel {get;设置;} = new TemplateModel();公共EditContext EditContext{获取;设置;} public List DropDownData {get;} = new List(){"保加利亚","意大利","希腊"};public List CityData {get {switch (MyModel.Country) {case "Bulgaria":返回新列表() {"Sofia", "Plovdiv", "Varna", "Burgas"};case "Italy":返回new List(){"罗马","米兰","那不勒斯","都灵"};case "Greece":返回new List() {"Athens", "Thessaloniki", "Patras", "Piraeos"}; default: return new List(); break; } } } protected override void OnInitialized() { EditContext = new EditContext(MyModel); base.OnInitialized(); } public class TemplateModel { public TemplateModel() { } [Required] public string Country { get; set; } [Required] public string City { get; set; } [Range(typeof(bool), "true", "true", ErrorMessage = "You must confirm first time.")] public bool FirstTime { get; set; } } private void Clear() { MyModel = new TemplateModel(); EditContext = new EditContext(MyModel); EditContext.AddDataAnnotationsValidation(); } }

上面代码片段的结果

模板的例子

向模板化的表单项添加验证消息

控件可以为模板化的表单项呈现验证消息TelerikValidationMessage

@ @using System.ComponentModel.DataAnnotations                 @code {public TemplateModel MyModel {get;设置;} = new TemplateModel();公共EditContext EditContext{获取;设置;} public List DropDownData {get; } = new List() { "Bulgaria", "Italy", "Greece" }; public List CityData { get { switch (MyModel.Country) { case "Bulgaria": return new List() { "Sofia", "Plovdiv", "Varna", "Burgas" }; case "Italy": return new List() { "Rome", "Milan", "Naples", "Turin" }; case "Greece": return new List() { "Athens", "Thessaloniki", "Patras", "Piraeos" }; default: return new List(); break; } } } protected override void OnInitialized() { EditContext = new EditContext(MyModel); base.OnInitialized(); } public class TemplateModel { public TemplateModel() { } [Required(ErrorMessage ="Select a Country")] public string Country { get; set; } [Required(ErrorMessage ="Select a City")] public string City { get; set; } [Range(typeof(bool), "true", "true", ErrorMessage = "You must confirm first time.")] public bool FirstTime { get; set; } } private void Clear() { MyModel = new TemplateModel(); EditContext = new EditContext(MyModel); EditContext.AddDataAnnotationsValidation(); } }

另请参阅

在本文中
Baidu
map