Hi, I'm having trouble getting my asp validation to work when I have a name attribute to pass data to my controller.
When I use the below code, my client side validation works fine, but I am not sending any data back to my controller (Because there is no name="" parameter):
<div class="form-group">
<label class="font-heading" asp-for="Email" id="ContactUsEmailHeading">Email</label>
<input asp-for="Email" class="form-control" id="ContactUsEmailField" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
When I add the name attribute to send data to the controller, the client side validation functions, but the validation messages do not display.
<div class="form-group">
<label class="font-heading" asp-for="Email" id="ContactUsEmailHeading">Email</label>
<input asp-for="Email" name="ContactUsEmailField" class="form-control" id="ContactUsEmailField" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
I thought it may be that the validation is displaying but the page is refreshing before I can see it. Upon checking, the method is not executing. So the validation is still working, just the messages are not showing.
My controller:
[HttpPost]
public async Task<IActionResult> SendMail(string ContactUsNameField, string ContactUsEmailField, string ContactUsSubjectField, string ContactUsMessageField, bool ContactUsTandCCheckbox)
{
DateTime FeedbackDateTime = DateTime.UtcNow;
if (ModelState.IsValid)
{
try
{
await SaveContactFormEntry.saveUserEntryAsync(FeedbackDateTime, ContactUsNameField, ContactUsEmailField, ContactUsSubjectField, ContactUsMessageField, ContactUsTandCCheckbox);
}
catch (Exception ex)
{
return RedirectToAction("Help");
}
}
return RedirectToAction("Help");
}
Could someone please help me to get my validation working while also passing data to the controller?