ModelState validation not showing which fields are invalid

1.3k views Asked by At

My ModelState validation is giving me very generic error messages, I would like to know exactly which fields are invalid.

Example

As you can see the first two textboxes "Startup rate < 1 min" and "Startup rate 1-3 min" are both empty, but the modelstate validation messages only say "The value '' is invalid". I would like it to say which fields exactly are invalid.

I placed the following line in my view: <div asp-validation-summary="All"></div>

This is my controller action and my model with required attributes:

[HttpPost]
public async Task<IActionResult> EditSubtitleSetting(EditSubtitleSettingsModel model)
{
    try
    {
        if (ModelState.IsValid)
        {
            await _subtitleSettingService.UpdateSubtitleSetting(model);
            return RedirectToAction("Subtitling");
        }
    } catch (CustomException e)
    {
        foreach (var m in e.Messages)
        {
            ModelState.AddModelError(m.Key, m.Message);
        }
    }
    return View(model);
}


public class EditSubtitleSettingsModel
{
    public string Id { get; set; }
    public string FromLanguage { get; set; }
    public string ToLanguage { get; set; }

    [Required(ErrorMessage = "Startup rate less than one minute is required")]
    public decimal StartupRateLessThanOneMinute { get; set; }

    [Display(Name = "Startup rate between one and three minutes")]
    [Required(ErrorMessage = "Startup rate between one and three minutes is required")]
    public decimal StartupRateBetweenOneAndThreeMinutes { get; set; }

    [Required(ErrorMessage = "Startup rate between three and five minutes is required")]
    public decimal StartupRateBetweenThreeAndFiveMinutes { get; set; }

    [Required(ErrorMessage = "Price per subtitle is required")]
    public decimal PricePerSubtitle { get; set; }

    [Required(ErrorMessage = "Default rate for translators is required")]
    public decimal DefaultRateTranslators { get; set; }
}

How can I have the validation message tell me which fields are invalid?

1

There are 1 answers

3
nbokmans On

Apparantly, required attribute only works on nullable decimals. So I changed my model to only have nullable decimals, and now it properly shows the validation messages. So a very easy fix.

This is what my model now looks like:

public class EditSubtitleSettingsModel
{
    public string Id { get; set; }
    public string FromLanguage { get; set; }
    public string ToLanguage { get; set; }

    [Required(ErrorMessage = "Startup rate less than one minute is required")]
    public decimal? StartupRateLessThanOneMinute { get; set; }

    [Required(ErrorMessage = "Startup rate between one and three minutes is required")]
    public decimal? StartupRateBetweenOneAndThreeMinutes { get; set; }

    [Required(ErrorMessage = "Startup rate between three and five minutes is required")]
    public decimal? StartupRateBetweenThreeAndFiveMinutes { get; set; }

    [Required(ErrorMessage = "Price per subtitle is required")]
    public decimal? PricePerSubtitle { get; set; }

    [Required(ErrorMessage = "Default rate for translators is required")]
    public decimal? DefaultRateTranslators { get; set; }
}