MVC4 input field placeholder

后端 未结 10 1258
北恋
北恋 2020-11-30 02:00

Does MVC4 by default support placeholders for generated input fields? I didn\'t found anything so I am trying to implement my own but unfortunately

10条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 02:24

    An alternative to using a plugin is using an editor template. What you need to do is to create a template file in Shared\EditorTemplates folder and call it String.cshtml. Then put this in that file:

    @Html.TextBox("",ViewData.TemplateInfo.FormattedModelValue, 
        new { placeholder = ViewData.ModelMetadata.Watermark })
    

    Then use it in your view like this:

    @Html.EditorFor(m=>Model.UnitPercent)
    

    The downside, this works for properties of type string, and you will have to create a template for each type that you want support for a watermark.

提交回复
热议问题