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 466 posts 1621 karma points c-trib
    6 days ago
    Ali Sheikh Taheri
    0

    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")]
        [Phone]
        public string ContactNumber { get; set; }
    
        [Display(Name = "Email Address")]
        [EmailAddress]
        [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" }))
    {
        @Html.AntiForgeryToken()
    
    <div class="form-group">
        <label for="@Html.IdFor(m => m.Name)">
            @Html.DisplayNameFor(m => m.Name)
            <span class="required">*</span>
        </label>
    
        @Html.ValidationMessageFor(m => m.Name, null, new { @class = "required" })
        @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
    </div>
    
    <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>
    
    <div class="form-group">
        <label for="@Html.IdFor(m => m.Email)">
            @Html.DisplayNameFor(m => m.Email)
            <span class="required">*</span>
        </label>
        @Html.ValidationMessageFor(m => m.Email, null, new { @class = "required" })
        @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
    </div>
    
    <div class="form-group">
        <label for="@Html.IdFor(m => m.Message)">
            @Html.DisplayNameFor(m => m.Message)
            <span class="required">*</span>
        </label>
        @Html.ValidationMessageFor(m => m.Message, null, new { @class = "required" })
        @Html.TextAreaFor(m => m.Message, new { @class = "form-control", rows = 6 })
    </div>
    
    <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.

    Thanks

    Ali

Please Sign in or register to post replies

Write your reply to:

Draft