MVC4 input field placeholder

后端 未结 10 1266
北恋
北恋 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:23

    There are such of ways to Bind a Placeholder to View:

    1) With use of MVC Data Annotations:

    Model:

    [Required]
    [Display(Prompt = "Enter Your First Name")]
    public string FirstName { get; set; }
    

    Razor Syntax:

    @Html.TextBoxFor(m => m.FirstName, new { placeholder = @Html.DisplayNameFor(n => n.UserName)})
    

    2) With use of MVC Data Annotations But with DisplayName:

    Model:

    [Required]
    [DisplayName("Enter Your First Name")]
    public string FirstName { get; set; }
    

    Razor Syntax:

    @Html.TextBoxFor(m => m.FirstName, new { placeholder = @Html.DisplayNameFor(n => n.UserName)})
    

    3) Without use of MVC Data Annotation (recommended):

    Razor Syntax:

    @Html.TextBoxFor(m => m.FirstName, new { @placeholder = "Enter Your First Name")
    

提交回复
热议问题