Copied to clipboard

Flag this post as spam?

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


  • Roger Withnell 128 posts 613 karma points
    Jun 08, 2015 @ 16:36
    Roger Withnell
    0

    Server side validation error

    I've built my first SurfaceController as per the excellent video chapter.

    The contact form works fine but, before I set up client validation (which works also) or if I disable client validation, server side validation ends with an error when Submit is clicked only if one or more of the form's fields are empty, as follows: 1. Open the page with the Contact Form. 2. Leave one of more fields empty. 3. Click Submit. 4. In debug, the controller recognises that the model is invalid: if (!ModelState.IsValid) 5. The page with the contact form renders but the macros in the page (the contact form macro and the navigation macro) do not load with the following messages embedded in the page: Error loading Partial View script (file: ~/Views/MacroPartials/Navigation.cshtml) and Error loading Partial View script (file: ~/Views/MacroPartials/ContactForm.cshtml)

    What am I doing wrong?

    Your help would be much appreciated.

    Thanking you in anticipation.

    Roger

  • Ifrahim Rasool 28 posts 84 karma points
    Jun 11, 2015 @ 10:14
    Ifrahim Rasool
    0

    Can you paste your code?

  • Roger Withnell 128 posts 613 karma points
    Jun 11, 2015 @ 11:47
    Roger Withnell
    0

    Thanks, Ifrahim. Herewith the code:

    Partial macro:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @Html.Action("Index", "ContactFormSurface")
    

    Model:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;
    
    namespace ParishMaster.Models
    {
        public class ContactFormViewModel
        {
            [Required]
            public string Name { get; set; }
            [Required]
            [EmailAddress]
            public string Email { get; set; }
            [Required]
            public string Message { get; set; }
        }
    }
    

    View:

    @model ParishMaster.Models.ContactFormViewModel
    
    @if (TempData["success"] == null)
    {
        using (Html.BeginUmbracoForm<ParishMaster.Controllers.ContactFormSurfaceController>("HandleFormSubmit"))
        {
            <div class="contact-form-label">
                Name:
            </div>
            <div class="contact-form-input">
                @Html.TextBoxFor(m => m.Name)<br />
                @Html.ValidationMessageFor(m => m.Name)
            </div>
            <div style="clear:both;"></div>
            <div class="contact-form-label">
                Email address:
            </div>
            <div class="contact-form-input">
                @Html.TextBoxFor(m => m.Email)<br />
                @Html.ValidationMessageFor(m => m.Email)
            </div>
            <div style="clear:both;"></div>
            <div class="contact-form-label">
                Message:
            </div>
            <div class="contact-form-input">
                @Html.TextAreaFor(m => m.Message)<br />
                @Html.ValidationMessageFor(m => m.Message)
            </div>
            <div style="clear:both;"></div>
            <div class="contact-form-label">
                &nbsp;
            </div>
            <div class="contact-form-input">
                <input type="Submit" name="Submit" value="Send your message">
            </div>
            <div style="clear:both;"></div>
        }
    
    }
    else
    {
        <p>
            Thank you.  We'll be in touch
        </p>
    }
    

    Controller:

    using ParishMaster.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Mail;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    
    namespace ParishMaster.Controllers
    {
        public class ContactFormSurfaceController : SurfaceController
        {
            // GET: ContactFormSurface
            public ActionResult Index()
            {
                return PartialView("ContactForm", new ContactFormViewModel());
            }
    
            [HttpPost]
            public ActionResult HandleFormSubmit(ContactFormViewModel model)
            {
                if (!ModelState.IsValid)
                    return CurrentUmbracoPage();
    
                //Send email
    
                var vToEmail = CurrentPage.GetProperty("dtpMasterSendContactFormToEmailAddress", true).Value;
                string vToEmailString = Convert.ToString(vToEmail);
                MailMessage message = new MailMessage("[email protected]", vToEmailString);
                message.Subject = "Contact Form request";
                message.Body = "Name: " + model.Name + ", Email: " + model.Email + ", Message: " + model.Message;
                SmtpClient smtp = new SmtpClient("mail.MyServer.com", 25);
                smtp.Credentials = new System.Net.NetworkCredential()
                {
                    UserName = "[email protected]",
                    Password = "password"
                };
                smtp.Send(message);
    
                TempData["success"] = true;
    
                return RedirectToCurrentUmbracoPage();
            }
        }
    }
    
  • Alex Skrypnyk 6162 posts 24132 karma points MVP 8x admin c-trib
    Jun 11, 2015 @ 12:56
    Alex Skrypnyk
    0

    Did you try to use RedirectToCurrentUmbracoPage ?

  • Roger Withnell 128 posts 613 karma points
    Jun 12, 2015 @ 08:46
    Roger Withnell
    0

    If I use RedirectToCurrentUmbracoPage, the page reloads without errors but the "required field" messages does not display alongside the text boxes.

  • Sabin Regmi 2 posts 71 karma points
    May 30, 2016 @ 06:31
    Sabin Regmi
    0

    Hi Roger,

    Did you find the solution?? I also have a same issue.

  • Paul Seal 524 posts 2889 karma points MVP 7x c-trib
    May 30, 2016 @ 07:09
    Paul Seal
    100

    I'm on my mobile here so bear with me. I can see your problem. The action result that loads your form creates a new model each time. You need to duplicate the method using the same name. Above the first one put [HttpGet] this one can be the one which uses a new model.

    Then above the duplicate one put [HttpPost] and inside the brackets get it to expect the model ie.

    [HttpPost] Public actionresult loadform(modelclassname model) Return partialview("partialviewname", model);

    The key here is that on the post back you are looking for the model and you use that model again instead of creating a new one.

Please Sign in or register to post replies

Write your reply to:

Draft