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)
-
0
Duplicate of #5219@a2c59c56-3b44-4df5-8ea9-db52d1a48520
That's how ASP.NET Core (not ABP) model binding works. The errors are correctly thrown from server because they are null.
You can implement custom model binding: https://docs.microsoft.com/en-us/aspnet/core/mvc/advanced/custom-model-binding?view=aspnetcore-2.0
By the way, that's a HTML Helper, not a Tag Helper: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro?view=aspnetcore-2.1#tag-helpers-compared-to-html-helpers
-
0
Yes of course Aaron, HTML Helpers. It was a late night!