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 127 posts 612 karma points
    Jun 10, 2015 @ 21:31
    Roger Withnell
    1

    Get a macro parameter in a Surface Controller

    I've built a Contact Form exactly as the video tutorial on the Surface Controller and use a Partial Macro to embed the form in a page.

    The Partial Macro has a parameter that gets the email address to which the completed Contact Form should be sent.

    The Surface Controller has the code to send the Contact Form to an SMTP server. How do I get the email address in the Partial Macro Parameter into the Surface Controller code?

    Your help would be much appreciated.

    Thanking you in anticipation.

    Roger

  • Alex Skrypnyk 6060 posts 23282 karma points MVP 6x admin c-trib
    Jun 11, 2015 @ 09:12
    Alex Skrypnyk
    0

    Hi Roger,

    Can you provide your macro partial view code ?

    You can pass your params via model class, or just use Request object like that - http://stackoverflow.com/questions/20151556/how-to-get-the-http-post-data-in-c

    Thanks, Alex

  • Roger Withnell 127 posts 612 karma points
    Jun 11, 2015 @ 11:38
    Roger Withnell
    0

    Thanks, Alex.

    In the Surface Controller code below, I currently get the destination email address from a document type property. I want to change this to get it from a parameter of the macro.

    Partial macro:

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

    The 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; }
        }
    }
    

    The 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>
    }
    

    Surface 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;
    

    //Get this email address from the macro parameter instead.

                    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.server.com", x);
                    smtp.Credentials = new System.Net.NetworkCredential()
                    {
                        UserName = "[email protected]",
                        Password = "password"
                    };
                    smtp.Send(message);
    
                    TempData["success"] = true;
    
                    return RedirectToCurrentUmbracoPage();
                }
            }
        }
    
  • Roger Withnell 127 posts 612 karma points
    Jun 23, 2015 @ 16:33
    Roger Withnell
    0

    I'm still unable to resolve this issue.

    I need to be able to access a PartialMacro parameter from within a Surface Controller to set the email address that a submitted Contact Form is sent to.

    The parameter is set up within the Macro - I can access it from within the PartialMacro but how do I access it from within the Surface Controller?

    Your help would be much appreciated.

    Thanking you in anticipation.

    Roger

  • David Specht 4 posts 25 karma points
    Jul 27, 2015 @ 18:23
    David Specht
    0

    Roger, Did you find a answer to this problem? I just ran into the same problem and the more I think about it I'm starting to believe it isn't possible to access the parameters. I don't think the Macro is called when the post happens. The post is directly routed to the controller independently of the macro.

    I don't think the macro is loaded and run until you call CurrentUmbracoPage or RedirectToCurrentUmbracoPage from within the controller.

    In my case I'm going to move the parameters I need into the page since the CurrentPage is accessible.

    Thanks, David

  • Roger Withnell 127 posts 612 karma points
    Jul 28, 2015 @ 14:25
    Roger Withnell
    0

    Hello, David.

    No, I didn't find an answer to this problem. But I need to. In my application the Content Editor can add the Contact Form onto any page so I can't add the destination email address property to a specific page.

    Can anybody else help with this issue, please?

    Roger

  • David Specht 4 posts 25 karma points
    Jul 28, 2015 @ 16:47
    David Specht
    1

    Roger, I can think of one solution that is a hack. You can pass the macro parameters to the controller when the form is rendered. Encrypt the email, to protect against tampering and pass it to the view to render it as a hidden field.

    Then on the post you decrypt the email from the submitted model.

    Partial macro:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var email = @Model.MacroParameters["email"];
    }    
    
     @Html.Action("Index", "ContactFormSurface", new {email=email})
    

    The 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; }
            [Required]
            public string DestEmail { get; set; }
        }
    }
    

    The 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)
                @Html.HiddenFor(m => m.DestEmail)
            </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>
    }
    

    Surface 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(string email)
            {
                            var viewModel = new ContactFormViewModel();
                            //set your own encryption keys, method, ...
                            viewModel.DestEmail = Encrypt(email);
                return PartialView("ContactForm", viewModel);
            }
    
            [HttpPost]
            public ActionResult HandleFormSubmit(ContactFormViewModel model)
            {
                if (!ModelState.IsValid)
                    return CurrentUmbracoPage();
    
                //Send email
                            //setup your own decryption
                var vToEmail = Decrypt(model.DestEmail);
    
                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.server.com", x);
                smtp.Credentials = new System.Net.NetworkCredential()
                {
                    UserName = "[email protected]",
                    Password = "password"
                };
                smtp.Send(message);
    
                TempData["success"] = true;
    
                return RedirectToCurrentUmbracoPage();
            }
        }
    }
    
  • Roger Withnell 127 posts 612 karma points
    Sep 16, 2015 @ 23:12
    Roger Withnell
    0

    Perfect, David. Worked a treat.

    Many thanks. Much appreciated.

    Roger

  • Roger Withnell 127 posts 612 karma points
    Jul 30, 2015 @ 16:19
    Roger Withnell
    100

    Many thanks, David.

    I'll try this out in the next few weeks.

    I'll be in touch.

    Roger

Please Sign in or register to post replies

Write your reply to:

Draft