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?
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.
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; }
}
}
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.
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?
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.
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.
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; }
}
}
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
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
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:
The Model:
The view:
Surface Controller
//Get this email address from the macro parameter instead.
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
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
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
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:
The Model:
The view:
Surface Controller:
Perfect, David. Worked a treat.
Many thanks. Much appreciated.
Roger
Many thanks, David.
I'll try this out in the next few weeks.
I'll be in touch.
Roger
is working on a reply...