0

I have created a web application using .Net MVC Core, with Code First approach, and generated the Views/Controllers through scaffolding.

For validation, I'm using Fluent API and Data Annotations, and everything seems to be working just fine. However my DataAnnotation's custom error messages, show up only on the client-side validation, and when I block my browser's JS to test my server-side validation, I get the following error message instead: "The value '' is invalid."

I tried to use [DataType(DataType.DateTime, ErrorMessage = "Invalid date!")] but it didn't work as well!

My Model:

    public class MyModel
{
    [Required(ErrorMessage = "Required field!")]
    [Display(Name = "Departure Date")]
    public DateTime DateDeparture { get; set; }

    [Required(ErrorMessage = "Required field!")]
    [Display(Name = "Arrival Date")]
    public DateTime DateArrival { get; set; }
 }

My View:

    public IActionResult Create()
    {
        return View();

    }

My Controller:

    @model App.Models.MyModel

@{
    ViewData["Title"] = "Create";
}

    <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="DateDeparture" class="control-label"></label>
                <input asp-for="DateDeparture" class="form-control" />
                <span asp-validation-for="DateDeparture" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="DateArrival" class="control-label"></label>
                <input asp-for="DateArrival" class="form-control" />
                <span asp-validation-for="DateArrival" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>

Client-Side Validation

Client-Side Validation

Server-Side Validation

Server-Side Validation

According to the suggested solution here for a similar scenario (Thread) I have to make my field nullable! Which doesn't seem to be a proper solution for all cases! And I already have another form with Non-Nullable string, that generates the same custom error message for both client & server sides validations, that's why I believe the issue is more related to the scalar values rather than Non/Nullable type options, but I can't figure out the reason, to be able to override that server-side validation message!

Any help would be appreciated…

  • Both your images are identical. Its not clear what you are asking –  Nov 13 '18 at 22:15
  • As a side note, the answer you linked to is wrong. A `DateTime` is required by default (although its recommended that reference types be nullable in your view model to protect against under-posting attacks - refer [https://stackoverflow.com/questions/43688968/what-does-it-mean-for-a-property-to-be-required-and-nullable/43689575#43689575](https://stackoverflow.com/questions/43688968/what-does-it-mean-for-a-property-to-be-required-and-nullable/43689575#43689575) –  Nov 13 '18 at 22:18
  • Updated.. Thank you @StephenMuecke – Odeh Khanfar Nov 14 '18 at 09:02

0 Answers0