Add unobtrusive validation to dropdown

M J 661 Reputation points
2021-11-22T18:20:12.337+00:00

This is a site for job applicants in the construction industry. I have localization on the public facing part of this site, so some users will see stuff in Spanish and other users will see it in English.

On one part of the application, the company I am building this for asks various questions about the applicants skill level in certain topics. Depending on the craft the applicant is applying to there are from 6 to 12 questions asked. On a desktop or laptop the applicants will see radio buttons under the apprpriate column to select their level of Experince (Experienced, Skilled, Developing, No Experience) However, on a mobile device I am using a dropdown list for them to select their experience from.

Because the different crafts have a different amount of questions asked I am stumped about how to add the data-val-required attribute in the correct language on the dropdown list.

They will all have at least 6 questions but it may be as many as 12 questions asked. Here is what i have in the model for the first 6 skills

[Required(ErrorMessageResourceType = typeof(Application), ErrorMessageResourceName = "MakeSelection")]
public int Skill1 { get; set; }

[Required(ErrorMessageResourceType = typeof(Application), ErrorMessageResourceName = "MakeSelection")]
public int Skill2 { get; set; }

[Required(ErrorMessageResourceType = typeof(Application), ErrorMessageResourceName = "MakeSelection")]
public int Skill3 { get; set; }

[Required(ErrorMessageResourceType = typeof(Application), ErrorMessageResourceName = "MakeSelection")]
public int Skill4 { get; set; }

[Required(ErrorMessageResourceType = typeof(Application), ErrorMessageResourceName = "MakeSelection")]
public int Skill5 { get; set; }

[Required(ErrorMessageResourceType = typeof(Application), ErrorMessageResourceName = "MakeSelection")]
public int Skill6 { get; set; }

Can I do the same for the next 6 skills that may or may not show depending on the craft? Will that still allow the use of

if(ModelState.IsValid)
{
//Add the answers
}

for the crafts that only have 6 questions if i have

[Required(ErrorMessageResourceType = typeof(Application), ErrorMessageResourceName = "MakeSelection")]
public int Skill7 { get; set; }

[Required(ErrorMessageResourceType = typeof(Application), ErrorMessageResourceName = "MakeSelection")]
public int Skill8 { get; set; }

[Required(ErrorMessageResourceType = typeof(Application), ErrorMessageResourceName = "MakeSelection")]
public int Skill9 { get; set; }

[Required(ErrorMessageResourceType = typeof(Application), ErrorMessageResourceName = "MakeSelection")]
public int Skill10 { get; set; }

[Required(ErrorMessageResourceType = typeof(Application), ErrorMessageResourceName = "MakeSelection")]
public int Skill11 { get; set; }

[Required(ErrorMessageResourceType = typeof(Application), ErrorMessageResourceName = "MakeSelection")]
public int Skill12 { get; set; }

If not, then if I don't use the [Required] DataAnnotation, is it possible to add the data-val-required attribute in the dropdown in the list of HTML attributes?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
{count} votes