Copied to clipboard

Flag this post as spam?

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


  • Jason Espin 368 posts 1335 karma points
    Oct 27, 2014 @ 17:26
    Jason Espin
    0

    ModelState always invalid despite correct information in form fields

    HI all,

    I am having some serious issues with Surface Controllers and getting my model to validate despite all of the information being in the structure I am expecting it to be.

    Here is the code for my model:

    public class EnquiryModel
        {
            [UmbracoRequired("Enquiry.Error.Required")]
            [UmbracoDisplayName("Enquiry.Name")]
            public string Name { get; set; }
    
            [UmbracoEmail(ErrorMessageDictionaryKey = "Enquiry.Error.Email")]
            [UmbracoRequired("Enquiry.Error.Required")]
            [UmbracoDisplayName("Enquiry.Email")]
            public string Email { get; set; }
    
            [UmbracoDisplayName("Enquiry.Telephone")]
            public int Telephone { get; set; }
    
            [UmbracoRequired("Enquiry.Error.Required")]
            [UmbracoDisplayName("Enquiry.Message")]
            public string Message { get; set; }
        }
    

    And my controller:

    public class EnquiryController : SurfaceController
        {
            [HttpPost]
            public ActionResult Submit(EnquiryModel model)
            {
                string nameValue = model.Name;
                string emailValue = model.Email;
                int telephoneValue = model.Telephone;
                string messageValue = model.Message;
    
                if (!ModelState.IsValid)
                    return CurrentUmbracoPage();
    
                // Create a regular expression to remove script tags
                Regex regex = new Regex(@"<script(.+?)*</script>");
                string request = regex.Replace(model.Message, string.Empty);
                request = request + "<br/><br/>" + "Phone: " + model.Telephone + "<br/><br/>" + "Email: " + model.Email;
    
                MailMessage message = new MailMessage();
                message.From = new MailAddress(model.Email);
                message.To.Add(new MailAddress("[email protected]"));
                message.Subject = "New Website Enquiry - " + model.Name;
                message.Body = request;
    
                SmtpClient client = new SmtpClient();
    
                try
                {
                    client.Send(message);
                    TempData["success"] = true;
                }
                catch (Exception ex)
                {
                    TempData["error"] = true;
                    return CurrentUmbracoPage();
                }
    
                return RedirectToCurrentUmbracoPage();
    
            }
    
        }
    

    And finally my view:

    @using (Html.BeginUmbracoForm<EnquiryController>("Submit"))
    {
        <div class="form-group col-md-6">
            @Html.LabelFor(model => model.Name, new { @class = "control-label"})
            @Html.TextBoxFor(model => model.Name, new { @class = "form-control", aria_required = "true" , required = "true", placeholder = "Name" })
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <div class="form-group col-md-6">
            @Html.LabelFor(model => model.Email, new { @class = "control-label" })
            @Html.TextBoxFor(model => model.Email, new { @class = "form-control", aria_required = "true", required = "true", type = "email", placeholder = "[email protected]"})
            @Html.ValidationMessageFor(model => model.Email)
        </div>
        <div class="form-group col-md-6">
            @Html.LabelFor(model => model.Telephone, new { @class = "control-label" })
            @Html.TextBoxFor(model => model.Telephone, new { @class = "form-control"})
        </div>
        <div class="form-group col-md-12">
            @Html.LabelFor(model => model.Message, new { @class = "control-label" })
            @Html.TextAreaFor(model => model.Message, new { @class = "form-control message", rows = "7", placeholder = "How can we help?" })
            @Html.ValidationMessageFor(model => model.Message)
        </div>
        <div class="form-group col-md-12">
            <button type="submit" class="btn btn-custom">@UmbracoValidationHelper.UmbracoHelper.GetDictionaryValue("Buttons.Send")</button>
        </div>
    }
    

    I have tested all of the values needed by the model and they are all correct yet the code always bugs out at the model validity checker. What is wrong here?

  • Tobias Klika 101 posts 570 karma points c-trib
    Oct 27, 2014 @ 17:41
    Tobias Klika
    0

    So all the values in the EnquiryModel are correctly assigned when posted to the server? Did you set a breakpoint before .IsValid and check this?

Please Sign in or register to post replies

Write your reply to:

Draft