Copied to clipboard

Flag this post as spam?

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


  • Ben Tice 31 posts 131 karma points
    Jul 16, 2014 @ 22:50
    Ben Tice
    1

    Using Macro Parameters in Surface Controller

    Hi All,

    Following the Umbraco.tv videos, I've created a contact form to be used via the RTE. The form/surface controller work great, but I want to parameterize some of the properties in my ViewModel. For the life of me, I can't find an example of how to use macro parameters with this setup.

    ViewModel

    public class ContactFormViewModel
    {
        [Required]
        public string FirstName { get; set; }

        [Required]
        public string LastName { get; set; }

        [Required]
        [EmailAddress]
        public string Email { get; set; }

        public string Phone { get; set; }

        [Required]
        public string Comments { get; set; }

        public readonly string EmailFrom
        {
            get
            {
                return umbraco.UmbracoSettings.NotificationEmailSender;
            }
        }
       
        //Would like to set these via macro parameters
        public int RedirectId { get; set; }
        public string EmailTo { get; set; }
        public string EmailSubject { get; set; }
    }

    SurfaceController

    public class ContactFormSurfaceController : SurfaceController
    {
        public ActionResult Index()
        {
            return PartialView("ContactForm", new ContactFormViewModel());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult HandleFormSubmit(ContactFormViewModel model)
        {
            if (!ModelState.IsValid)
                return CurrentUmbracoPage();

            string strEmailBody = "Hello World";

            MailMessage msg = new MailMessage(model.EmailFrom, model.EmailTo);
            msg.Subject = model.EmailSubject;
            msg.Body = strEmailBody;
            msg.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Send(msg);

            return RedirectToUmbracoPage(model.RedirectId);
        }

    }

    Partial

    @model MyProject.Models.ContactFormViewModel

    @using (Html.BeginUmbracoForm("HandleFormSubmit", null, new { @class="form" }))
    {
        @Html.AntiForgeryToken()

        @Html.TextBoxFor(m => m.FirstName, new { placeholder = "First Name*" })
        @Html.TextBoxFor(m => m.LastName, new { placeholder = "Last Name*" })
        @Html.TextBoxFor(m => m.Email, new { placeholder = "Email Address*" })
        @Html.TextBoxFor(m => m.Phone, new { placeholder = "Phone Number" })
        @Html.TextAreaFor(m => m.Comments, new { placeholder = "Comments*" })

       
    }

    MacroPartial

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

    At what point can I grab properties from the macro?

    Any insight would be greatly appreciated! :)

    Thanks,
    Ben

  • Ben Tice 31 posts 131 karma points
    Jul 17, 2014 @ 22:04
    Ben Tice
    1

    Ok, I've found an example (https://www.youtube.com/watch?v=eovAHqxoZZU) that's passing the macro parameter value into the model, and then uses hidden fields to post back to the surface controller. Is there any way to retain these values without using hidden fields?

    Thanks!
    Ben

  • Joel Hansen 38 posts 96 karma points
    Sep 07, 2014 @ 00:32
    Joel Hansen
    0

    Hello Ben

    Did you find a solution for this?

    Kind regards

    Joel

  • Ben Tice 31 posts 131 karma points
    Sep 08, 2014 @ 15:08
    Ben Tice
    1

    Hi Joel,

    Unfortunately, no, I haven't found a solution to this. I settled on hidden fields for now, but ideally there would be a straight-forward way to pass these parameters encrypted. Hopefully this will be further documented in the future. If I find a solution, I will certainly update this post. Good luck!

    Ben

  • Bjarke Petersen 15 posts 113 karma points
    Nov 11, 2014 @ 15:58
    Bjarke Petersen
    0

    Couldn't you do it like this (VB though) Controller:

    Function Index(ByVal questionNumber As Integer, ByVal question As String) As ActionResult
                Dim myModel As Object = New CsiEntryViewModel With {.QuestionNumber = questionNumber, .Question = question}
                Return PartialView("~/views/Partials/CsiEntryView.vbhtml", myModel)
            End Function
    

    In the Macro Partial:

    @Html.Action("Index", "CsiEntrySurface", new { questionNumber = @Model.MacroParameters["questionNumber"], question = @Model.MacroParameters["question"]})
    

    It might be bad practice - I don't know...

  • Ben Tice 31 posts 131 karma points
    Sep 12, 2015 @ 17:26
    Ben Tice
    101

    This recently came up for me again and I've gotten around the issue with the following changes:

    MacroPartial

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @Html.Partial("ContactForm", new MyProject.Models.ContactFormViewModel { EmailSubject = @Model.MacroParameters["EmailSubject"].ToString(), EmailTo = @Model.MacroParameters["EmailTo"].ToString(), RedirectId = int.Parse(@Model.MacroParameters["RedirectId"].ToString()) })
    

    Partial (modified @using...)

    @using (Html.BeginUmbracoForm<MyProject.Controllers.ContactFormSurfaceController>("HandleFormSubmit", new { EmailTo = Model.EmailTo, EmailSubject = Model.EmailSubject, RedirectId = Model.RedirectId }, new { @class = "form" }))
    

    Basically, the MacroPartial has access to the macro parameters, so I'm passing them to the partial in the ContactFormViewModel object.

    In the partial, I'm using the "AdditionalRouteVals" parameter to pass the values to the surface controller at which point they're available in the model, i.e. model.EmailSubject.

    Hope this helps others! :)

Please Sign in or register to post replies

Write your reply to:

Draft