使用模板化助手和非模板化助手时,数据类型与必需的数据注释
我已经为我的模型对象定义了以下数据注释:-
[DataType(DataType.MultilineText)]
[Required(ErrorMessage = "Book Name is required !!!")]
public string BookName { get; set; }
我使用以下内容在视图中渲染模型:-
@Html.EditorFor(model => model.BookName)
但是当我将上面的内容更改为以下内容时:-
@Html.TextBoxFor(model => model.BookName)
然后只有 [Required(ErrorMessage = "Book姓名为必填!!!")]
正在工作。所以我认为如果我使用非模板化助手(TextAreaFor),Required 和 DataType 都不起作用,那么为什么 [Required] 在这种情况下起作用,而 DataType 不起作用? BR
i have defined the following data annotations for my model object:-
[DataType(DataType.MultilineText)]
[Required(ErrorMessage = "Book Name is required !!!")]
public string BookName { get; set; }
i am rendering the model at the view using the following:-
@Html.EditorFor(model => model.BookName)
but when i changed the above to the following:-
@Html.TextBoxFor(model => model.BookName)
then only the [Required(ErrorMessage = "Book Name is required !!!")]
was working . so i thought that both the Required and the DataType would not work if i use the Non Templated helper (TextAreaFor),, so why the [Required] worked in this case while the DataType did not?.
BR
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于使用
TextBoxFor
帮助器,MultileneText 属性将不起作用。如果您使用EditorFor
帮助程序,该帮助程序会将属性呈现为 textarea html 输入。但是,您无需使用呈现为的
TextBoxFor
帮助器来对呈现文本区域进行任何更改。因此多行属性无法再分配给输入。
MultileneText attribute wont work because of using
TextBoxFor
helper. If you were useEditorFor
helper, that helper renders property as an textarea html input. But you dont give any change to render textarea to that helper by usingTextBoxFor
helper which renders as<input type='text'/>
.So multiline attribute couldnt assign to input anymore.