MVC4 input field placeholder

后端 未结 10 1231
北恋
北恋 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:06
     @Html.TextBoxFor(m => m.UserName, new { @class = "form-control",@placeholder = "Name"  })  
    
    0 讨论(0)
  • 2020-11-30 02:08

    I did so

    Field in model:

    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }
    

    Razor:

    <li>
      @Html.TextBoxFor(m => m.UserName, new { placeholder = Html.DisplayNameFor(n => n.UserName)})
    </li>
    
    0 讨论(0)
  • 2020-11-30 02:12

    Try this:

    @Html.TextbBoxFor(x=>x.Email,new { @placeholder=@viewBag.email}
    

    If this possible or else what could be the way

    0 讨论(0)
  • 2020-11-30 02:17

    Of course it does:

    @Html.TextBoxFor(x => x.Email, new { @placeholder = "Email" })
    
    0 讨论(0)
  • 2020-11-30 02:19

    This works:

    @Html.TextBox("name", null,  new { placeholder = "Text" })
    
    0 讨论(0)
  • 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")
    
    0 讨论(0)
提交回复
热议问题