-1

How can I add the form-control class to this TextBoxFor? I'm new to coding.

<div>
    <div class="form-group text-center">
        <Label class="center-block">Hours</Label>
    </div>
    <div style="margin-top:-1%" class="form-group text-center">
        @Html.TextBoxFor(model => model.hours, new { type = "number", min = "0", step = "1", htmlAttributes = new { @class = "form-control center-block" } })
        @Html.ValidationMessageFor(model => model.hours, "", new { @class = "text-danger" })
    </div>
</div>

Result:

Result

halfer
  • 19,824
  • 17
  • 99
  • 186
Maroche
  • 3
  • 2
  • 1
    Does this answer your question? [ASP.NET MVC 3 Razor - Adding class to EditorFor](https://stackoverflow.com/questions/4576209/asp-net-mvc-3-razor-adding-class-to-editorfor) – demo May 27 '20 at 14:08

1 Answers1

0

Not sure where htmlAttributes is coming from here, but you're already within the context of setting HTML attributes in that object, such as type and min and step. You're also setting @class in your ValidationMessageFor, why not apply that same standard?

Just remove the htmlAttributes and set the @class property on the object that has the other attributes:

@Html.TextBoxFor(
  model => model.hours,
  new {
    type = "number",
    min = "0",
    step = "1",
    @class = "form-control center-block"
  })
David
  • 208,112
  • 36
  • 198
  • 279