Last Updated: 14 Jun 2021 14:38 by ADMIN
Created by: Murat
Comments: 2
Category: TextBox
Type: Feature Request

Feature Request

Create TexBox label from ViewModel Data Annotation

Currently, this is possible using the default @Html.Label, and @Html.Textbox


     [DisplayName("First Name :")]

public string FirstName { get; set; }


                @ Html.LabelFor(m=>m.FirstName)

Last Updated: 11 Jun 2021 05:56 by ADMIN
Created by: Eyup
Comments: 0
Category: TextBox
Type: Feature Request

Data Model

public string UserPassword { get; set; }
Razor HTML
<div class="form-group">
  <kendo-textbox name="UserPassword" type="password"> <!-HOW to use DataType Annotation here? -- >
      <textbox-label content="@Html.DisplayNameFor(m=>m.UserPassword)" floating="true" />
This scenario is working for a regular <input asp-for="FieldName"/> so it also should work for Telerik TextBox widget as well.


The solution below can be used as a workaround for the time being:

@{ Dictionary<string, string> fieldTypes = new Dictionary<string, string>();
    var properties = typeof(TestModel).GetProperties();
    foreach (var property in properties)
        var dataTypeAttributes = property.GetCustomAttributes(
            typeof(System.ComponentModel.DataAnnotations.DataTypeAttribute), false);
        if (dataTypeAttributes.Length > 0)
            var attribute = dataTypeAttributes[0] as
            var dataType = (System.ComponentModel.DataAnnotations.DataType)attribute.DataType;
            fieldTypes[property.Name] = dataType.ToString().ToLowerInvariant();
            fieldTypes[property.Name] = null;
    } }
<form id="mainForm" kendo-validator="true">
    <kendo-textbox for="UserPassword" type="@fieldTypes["UserPassword"]">
        <textbox-label content="@Html.DisplayNameFor(m=>m.UserPassword)" floating="true" />
    $(document).ready(function () {
        var validator = $("#mainForm").data("kendoValidator");