0

Inside razor view I have textbox rendered by razor helper

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

I want to add css id class together with this form-control class so I tried

@Html.EditorFor(model => model.Caption, 
        new { htmlAttributes = new { @class = "form-control", @id="myId" } })

but this doesnt work.

user1765862
  • 13,635
  • 28
  • 115
  • 220
  • 1
    Have you tried `@Html.EditorFor(model => model.Caption, new { @class = "form-control", @id="myId" } )`? – adricadar Mar 28 '15 at 15:50

2 Answers2

0

You have to use TextBoxFor() if you want to specify htmlAttributes:

@Html.TextBoxFor(model => model.Caption, 
    new { @class = "form-control" } )

Have a look at this SO post(Dimitro's Answer)

Community
  • 1
  • 1
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

By default you have an id, is Caption.

If you want to change it, acccording to this answer you have to change from EditorFor to TextBoxFor. To do this, we need to remove new { htmlAttributes because TextBoxFor expects to get htmlAttributes and EditorFor expects to get additionViewData.

@Html.TextBoxFor(model => model.Caption, new { @class = "form-control", @id="myId" } )

I want to mention that your code, as it is, changed the id when i tested.

@Html.EditorFor(model => model.Caption, 
    new { htmlAttributes = new { @class = "form-control", @id="myId" } })
Community
  • 1
  • 1
adricadar
  • 9,971
  • 5
  • 33
  • 46