Base solution for your next web application
Open Closed

Using Strongly Typed TagHelpers #5373


User avatar
0
Mitch created

I'm using the ASP Net Zero Tools to generate my ViewModels etc.

I prefer to use strongly typed TagHelpers, so I convert a lot of the generated form inputs to TagHelpers. All works as expected except for the Validation.

For example, the Zero Tools creates a ViewModel called CreateOrEditClientCaseModalViewModel.

public class CreateOrEditClientCaseModalViewModel
    {
       public CreateOrEditClientCaseDto ClientCase { get; set; }

	   	public string ClientName { get; set;}

        public bool IsEditMode => ClientCase.Id.HasValue;
    }

To bind to data in my Dto I want to use the following standard approach

@Html.TextBoxFor(m => m.ClientCase.ClientName)

However, if this is a required field the Validation will reject it with the following message "The ClientName is a required field" so it's obviously looking for "ClientName " and not "ClientCase.ClientName" or "ClientCase_ClientName".

It seems that the Validation is taking place against the Dto and not the ViewModel.

So my question is, how do I change it to get the Validation to use the ViewModel and not the Dto?


2 Answer(s)