Is it possible to set type of input generated by TextBoxFor

前端 未结 4 1804
感情败类
感情败类 2021-02-12 03:42

I am using ASP.NET MVC 3 TextBoxFor in a form and would like to use type=\"email\" for easier input for at least some mobile devices but cannot find how to set it using TextBoxF

相关标签:
4条回答
  • 2021-02-12 04:19

    Try adding [DataType( DataType.EmailAddress )] to the email property.

    [DataType( DataType.EmailAddress )]
    [StringLength(50)]
    public string Email { get; set; }
    
    0 讨论(0)
  • 2021-02-12 04:20
    [StringLength(50)]
    [DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email Address")]
    public string EmailAddress { get; set; }
    

    Try to add this. I think it works.

    0 讨论(0)
  • 2021-02-12 04:33

    Try to use

    @Html.TextBoxFor(m => m.Email, new { @type = "email" })
    

    http://msdn.microsoft.com/en-us/library/ee703538.aspx (htmlAttributes)

    0 讨论(0)
  • 2021-02-12 04:33

    You're using it the wrong way.

    Use EditorFor in View:

    @Html.LabelFor(m => m.Email)
    @Html.EditorFor(m => m.Email)
    

    In model, add the [DataType( DataType.EmailAddress )] Data Annotations property:

    [DataType( DataType.EmailAddress )]
    public string Email { get; set; }
    
    0 讨论(0)
提交回复
热议问题