简体   繁体   English

为什么 html.textbox 不生成 data-val 属性而 html.textboxFor 呢?

[英]why doesn't html.textbox generate data-val attributes while html.textboxFor does?

I have a form in a razor page where im implementing clientside validation using jquery, jquery unobtrusive and jquery validation alongside dataannotaions to validate the model.我在 razor 页面中有一个表单,其中我使用 jquery、jquery 不显眼和 jquery 验证以及数据注释来实现客户端验证以验证 model。

Using strongly typed textboxes i get the data-val attributes that i need for displaying errors using ValidationMessage() method.使用强类型文本框,我获得了使用 ValidationMessage() 方法显示错误所需的数据值属性。

HOWEVER when i use the loosely typed textbox the data-val attributes does not get generated, why is this?但是,当我使用松散类型的文本框时,不会生成 data-val 属性,这是为什么?

it is important that it works for Html.Textbox() since im using a more complex data structure and need to define specifik names for each element in the list.它适用于 Html.Textbox() 很重要,因为我使用更复杂的数据结构并且需要为列表中的每个元素定义特定名称。

loosly typed textbox and validationMessage:松散键入的文本框和 validationMessage:

在此处输入图像描述

Generated html:生成 html:

在此处输入图像描述

Strongly typed textbox and validationMessage:强类型文本框和验证消息:

在此处输入图像描述

Generated html:生成 html:

在此处输入图像描述

we can clearly see the data-val attributes here我们可以在这里清楚地看到 data-val 属性

The name field is built up like this (where key is a guid uniqe to each item in the list): name 字段是这样构建的(其中 key 是列表中每个项目的 guid uniqe):

在此处输入图像描述

how come the data-val attributes don't appear with loosly typed textboxes?为什么 data-val 属性不会出现在松散键入的文本框中?

I think i know the answer, since it is strongly typed we have information about the dataannotation in the model, whereas the loosley type just generates html from a name.我想我知道答案,因为它是强类型的,我们在 model 中有关于数据注释的信息,而 loosley 类型只是从名称生成 html。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM