Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 08, 2019 @ 08:45
    Ali Sheikh Taheri

    ModelState.IsValid is always true in Umbraco v8.2

    Hi all,

    I have a simple form and a simple view model class with required attributes as follow:

     using System.ComponentModel.DataAnnotations;
    public class ContactUsForm : BaseForm
        [Display(Name = "Name")]
        [Required(ErrorMessage = "Name is required!")]
        public string Name { get; set; }
        [Display(Name = "Contact Number")]
        public string ContactNumber { get; set; }
        [Display(Name = "Email Address")]
        [Required(ErrorMessage = "Email Address is required!")]
        public string Email { get; set; }
        [Display(Name = "Message")]
        [Required(ErrorMessage = "Message is required!")]
        public string Message { get; set; }

    and then in the view I have this:

    @using (Html.BeginUmbracoForm("Submit", "ContactUsFormSubmit", null, new { @class = "contact-form" }))
    <div class="form-group">
        <label for="@Html.IdFor(m => m.Name)">
            @Html.DisplayNameFor(m => m.Name)
            <span class="required">*</span>
        @Html.ValidationMessageFor(m => m.Name, null, new { @class = "required" })
        @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
    <div class="form-group">
        <label for="@Html.IdFor(m => m.ContactNumber)">
            @Html.DisplayNameFor(m => m.ContactNumber)
        </label> <span class="form-label-desc">(optional)</span>
        @Html.ValidationMessageFor(m => m.ContactNumber, null, new { @class = "required" })
        @Html.TextBoxFor(m => m.ContactNumber, new { @class = "form-control" })
    <div class="form-group">
        <label for="@Html.IdFor(m => m.Email)">
            @Html.DisplayNameFor(m => m.Email)
            <span class="required">*</span>
        @Html.ValidationMessageFor(m => m.Email, null, new { @class = "required" })
        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
    <div class="form-group">
        <label for="@Html.IdFor(m => m.Message)">
            @Html.DisplayNameFor(m => m.Message)
            <span class="required">*</span>
        @Html.ValidationMessageFor(m => m.Message, null, new { @class = "required" })
        @Html.TextAreaFor(m => m.Message, new { @class = "form-control", rows = 6 })
    <button class="btn btn-primary pull-right">Send Message</button>

    When the form is posted the ModelState.IsValid is always true, even all of the fields are empty.

    The same form used to work in Umbraco 7.15.3 but not in Umbraco 8.2

    So I am not sure if the problem is with Umbraco version 8.2 or I am missing something in the code.



  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 20, 2019 @ 09:28
    Ali Sheikh Taheri

    Just to make sure the problem is not with Umbraco I have installed a brand new version 8.2 and had a simple form and the validation worked like a charm.

    So that meant the validation issue is very specific to my solution. after a lot of Googling, I realised my solution (as it was a migration from v7 to v8.2) has a reference to Ninject Dependency Injection as well as LightInject which is the new DI that is shipped with Umbraco 8.

    So by getting rid of all NuGet packages related to Ninject and removing the code, finally the validation started to work Yayyyy!!!

Please Sign in or register to post replies

Write your reply to: