0

Hi Im new to MVC5 and I just want to ask how can I add the attribute for my Editor so i can add phone mask in it

Heres the code

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

and heres the attribute that i want to add

data-mask="(999) 999-9999"

I tried it in a simple input and it works

 <input type="text" class="form-control" data-mask="(999) 999-9999" placeholder="Phone">

Thanks in advance

Enrique Gil
  • 754
  • 4
  • 19
  • 50
  • 1
    `new { htmlAttributes = new { @class = "form-control", data_mask = "(999) 999-9999" } }` (underscore will be converted to hyphen by razor engine) –  Feb 20 '17 at 06:31
  • 1
    @StephenMuecke what is `data_mask`? just curious. – CodingYoshi Feb 20 '17 at 06:34
  • 1
    @CodingYoshi, A attribute used by a jquery plugin I assume, but I don't which one :) –  Feb 20 '17 at 06:39
  • @StephenMuecke i just tried data_mask and it doesn't work. – Enrique Gil Feb 20 '17 at 06:46
  • @CodingYoshi data-mask is a plugin by jasny – Enrique Gil Feb 20 '17 at 06:47
  • 1
    What does not work? - It will add `data-mask="(999) 999-9999"` assuming your using MVC-5.1+. Otherwise use `@Html.TextBoxFor(m => m.PhoneNumber, new { @class = "form-control", data_mask = "(999) 999-9999", placeholder = "Phone" })` –  Feb 20 '17 at 06:50
  • @StephenMuecke Thank you ! It is that i didnt include the package in global startapp – Enrique Gil Feb 21 '17 at 02:00

1 Answers1

0
new { htmlAttributes = new { @class = "form-control", data_mask = "(999) 999-9999" } }

(underscore will be converted to hyphen by razor engine)

great answer by user3559349

THess
  • 1,003
  • 1
  • 13
  • 21