Html 是否可以设置由 TextBoxFor 生成的输入类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12267108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:39:22  来源:igfitidea点击:

Is it possible to set type of input generated by TextBoxFor

asp.net-mvc-3html

提问by d456

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 TextBoxFor. Is this not easily possible?

我在表单中使用 ASP.NET MVC 3 TextBoxFor 并希望使用 type="email" 以便至少为某些移动设备更轻松地输入,但找不到如何使用 TextBoxFor 设置它。这不是很容易吗?

In View

在视图中

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

In model

在模型中

[StringLength(50)]
public string Email { get; set; }

(Already using a data annotation to protect size constraint in DB)

(已经使用数据注释来保护 DB 中的大小约束)

回答by Xordal

Try to use

尝试使用

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

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

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

回答by Toan

You're using it the wrong way.

你以错误的方式使用它。

Use EditorFor in View:

在视图中使用 EditorFor:

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

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

在模型中,添加[DataType( DataType.EmailAddress )]数据注释属性:

[DataType( DataType.EmailAddress )]
public string Email { get; set; }

回答by Forty-Two

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

尝试添加[DataType( DataType.EmailAddress )]到电子邮件属性。

[DataType( DataType.EmailAddress )]
[StringLength(50)]
public string Email { get; set; }

回答by arnoldrob

[StringLength(50)]
[DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email Address")]
public string EmailAddress { get; set; }

Try to add this. I think it works.

尝试添加这个。我认为它有效。