This is a migrated thread and some comments may be shown as answers.

EditorFor ignoring htmlAttributes

3 Answers 2495 Views
TextBox
This is a migrated thread and some comments may be shown as answers.
Stefan
Top achievements
Rank 1
Iron
Iron
Iron
Stefan asked on 03 Aug 2020, 01:24 PM

We have fields of type 'string' in our application which are defined like this:

@Html.EditorFor(model => model.Field, new { htmlAttributes = new { @class = "form-control ", @readonly = "readonly" } })

 

 

When we recently switched to the Telerik EditorTemplates we realized that the htmlAttributes weren't applied anymore.

Other fields, for example of type 'integer', still work as before.

 

When we deleted the EditorTemplate 'String.cshtml' the htmlAttributes were applied again, so this template seems to have been used, but just ignored the htmlAttributes.

 

Is this behaviour expected, or do we miss anything?

3 Answers, 1 is accepted

Sort by
0
Anton Mironov
Telerik team
answered on 05 Aug 2020, 01:16 PM

Hi, Stefan,

Thank you for the provided code snippet and details.

In order to apply the HtmlAttributes in the EditorTemplate try the approach below:

@(Html.Kendo().DropDownList()
    .Name("Employee") // The name of the widget has to be the same as the name of the property.
    .DataValueField("EmployeeID") // The value of the drop-down is taken from the EmployeeID property.
    .DataTextField("EmployeeName") // The text of the items is taken from the EmployeeName property.
    .BindTo((System.Collections.IEnumerable)ViewData["employees"]) // A list of all employees which is populated in the controller.
    //Add your HtmlAttributes
    .HtmlAttributes(new { htmlAttributes = new { @class = "form-control ", @readonly = "readonly" } })
)

I hope this information helps.

Regards,
Anton Mironov
Progress Telerik

0
Stefan
Top achievements
Rank 1
Iron
Iron
Iron
answered on 06 Aug 2020, 06:41 AM

Hi Anton,

thank you for the info.

 

The default EditorTemplate 'String.cshtml' provided by Telerik looks like this:

@model object
 @Html.Kendo().TextBoxFor(model => model)

 

The problem is, that when I edit this file, the htmlAttributes would apply to all my Textboxes, which I don't want.

Does this mean I cannot use 'EditorFor' and have to use 'Kendo().TextBoxFor' directly in my views?

 

Kind regards

0
Anton Mironov
Telerik team
answered on 07 Aug 2020, 03:58 PM

Hi, Stefan,

Thank you for the additional question.

You are absolutely correct - the TextBoxFor will work for one property out of the box in this case. The EditorTemplate has unified functionality as well. Try to use the .Name property. This will be a working approach for setting it only for the needed field(the .Name is also set as an Id of the widget). Here is an example:

 .Name("Employee") // The name of the widget has to be the same as the name of the property.

I hope this information helps.

Kind Regards,
Anton Mironov
Progress Telerik

Tags
TextBox
Asked by
Stefan
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Anton Mironov
Telerik team
Stefan
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or