使用模板化助手和非模板化助手时,数据类型与必需的数据注释

发布于 2025-01-02 16:53:00 字数 555 浏览 1 评论 0原文

我已经为我的模型对象定义了以下数据注释:-

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与往事干杯 2025-01-09 16:53:00

由于使用 TextBoxFor 帮助器,MultileneText 属性将不起作用。如果您使用 EditorFor 帮助程序,该帮助程序会将属性呈现为 textarea html 输入。但是,您无需使用呈现为 TextBoxFor 帮助器来对呈现文本区域进行任何更改。

因此多行属性无法再分配给输入。

MultileneText attribute wont work because of using TextBoxFor helper. If you were use EditorFor helper, that helper renders property as an textarea html input. But you dont give any change to render textarea to that helper by using TextBoxFor helper which renders as <input type='text'/>.

So multiline attribute couldnt assign to input anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文