Copied to clipboard

Flag this post as spam?

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


  • Ben Weeks 14 posts 35 karma points
    Oct 30, 2015 @ 13:51
    Ben Weeks
    0

    Returning a Partial View (ActionResult HandleFormPost(ContactModel model))

    Hi all,

    I have a pop-up form as follows (which uses FancyBox to display a certain DIV in the DOM that is previously hidden and contains a Partial):

    FancyBox pop-up

    In my surface controller I have:

        /// <summary>
        /// For more information, see:
        /// http://stackoverflow.com/questions/6282049/form-with-attachments-upload-and-email-sending
        /// </summary>
        /// <param name="model">The popup form model</param>
        /// <returns>The ActionResult</returns>
        public ActionResult HandleFormPost(PopupFormModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // Then good to submit.
                    var sb = new StringBuilder();
                    sb.AppendFormat("<p>Name: {0}</p>", model.Name);
                    sb.AppendFormat("<p>Email: {0}</p>", model.Email);
                    sb.AppendFormat("<p>{0}</p>", model.Message);
    
                    using (var client = new SmtpClient())
                    {
                        EnquiryType enquiryType = Enquiries.GetEnquiryByID(model.EnquiryType);
    
                        if (enquiryType != null)
                        {
                            var mail = new MailMessage();
                            mail.To.Add(enquiryType.EmailAddress);
                            mail.Subject = "Website Form Submission";
                            mail.Body = sb.ToString();
                            if (model.Attachment.HasFile())
                            {
                                var attachment = new Attachment(model.Attachment.InputStream, model.Attachment.FileName);
                                mail.Attachments.Add(attachment);
                            }
                            client.Send(mail);
                            return PartialView("Webtechy_Success");
                        }
                        return PartialView("Webtechy_Error");
                    }
                }
                return CurrentUmbracoPage(); // Not valid, there is something they need to fill in.
            }
            catch(Exception ex)
            {
                return PartialView("Webtechy_Error");
            }
        }
    

    However, rather than just updating the contents of the pop-up box, it's returning the Partial view taking up the whole page.

    Any ideas greatly appreciated.

    Regards,

    Ben

  • Sanjay 7 posts 27 karma points
    Jun 26, 2016 @ 12:23
    Sanjay
    0

    Hi - did you figure this out. Im having the same problem with my action result returning a partial view but it takes the whole page.

  • Jon R. Humphrey 164 posts 455 karma points c-trib
    Jun 26, 2016 @ 13:59
    Jon R. Humphrey
    1

    Ben,

    Basically you need to submit to the controller via Ajax and depending on the result show an appropriate response container.

    I'm not at my desk now but if you look at the example in the fancybox site you can see what I mean:

    Step 5: http://fancybox.net/blog

    I've got something like this in one of my sites, when I'm back at my desk tomorrow morning I'll find it for you and share the code!

    Jon

Please Sign in or register to post replies

Write your reply to:

Draft