简体   繁体   中英

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.

Using strongly typed textboxes i get the data-val attributes that i need for displaying errors using ValidationMessage() method.

HOWEVER when i use the loosely typed textbox the data-val attributes does not get generated, why is this?

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.

loosly typed textbox and validationMessage:

在此处输入图像描述

Generated html:

在此处输入图像描述

Strongly typed textbox and validationMessage:

在此处输入图像描述

Generated html:

在此处输入图像描述

we can clearly see the data-val attributes here

The name field is built up like this (where key is a guid uniqe to each item in the list):

在此处输入图像描述

how come the data-val attributes don't appear with loosly typed textboxes?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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