0

I want to change the class/style of my @Html.EditorFor field. I read something about customizing the template, but there is no template folder in my porject (Views/Shared/ONLY_FILES_HERE). Sadly I am NOT working with MVC 5.1.

Also I DON'T want to use TextBoxFor, because of inputvalidation.

How can I achieve this?

Here is a snippet of my model:

public class CreateTableColumnModels
{
    [RegularExpression(@"^[A-Za-z0-9]+$", ErrorMessage = "Use letters and numbers only please")]
    [Display(Name = "Name")]
    public string Name { get; set; }

I tried this:

@model QlikViewStammdaten.Models.CreateTableColumnModels
<div>
    @Html.EditorFor(x => Model.Name, new { @class = "form-control" })
</div>
David Walser
  • 183
  • 20

1 Answers1

1

As already mentioned in the comments, it is better to add a class to Html.TextBoxFor and NOT @Html.EditorFor. Why? because @Html.EditorFor renders a template and that template could contain multiple controls (now to which one of those controls do you want to add the class?)... see Adding a class to EditorFor

If you want numeric type, then add @type = "number"

@Html.TextBoxFor(m => m.Name, new { @class = "form-control", @type = "number"  }) 

Note: it is not x => Model.Name but m => m.Name

I am sure you already know this, but [Display(Name = "Name")] is redundant in your model, as the default display name is the variable name itself.

Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137