Copied to clipboard

Flag this post as spam?

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


  • Andreas Kristensen 20 posts 79 karma points
    Jul 20, 2012 @ 11:27
    Andreas Kristensen
    0

    Problems expanding Contact page

    Hello, i am trying to make the contact page from uBootstrap send mails with a subject that includes the querystring of the page.

    I use the same method as is used to replace brackets in the message to replace a bracket in the subject field with the querystring.

    I pass the querystring to the macro as a parameter via [@subject].

    Now, the macro displays @Parameter.subject with no problem when i render the contact form, i cant get the parameter into the SendForm function.

    @using System.Text
    @using System.Collections.Generic;
    @using Bootstrap.Logic.Utils
    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @{
        dynamic form = Context.Request.Form.ToDynamic();
        if (IsPost)
        {
            if (!Context.IsValidAntiForgery()) { ModelState.AddFormError(@Dictionary.InvalidPost); }
            if (MainHelper.IsEmpty(form.Name)) { ModelState.AddError("name", @Dictionary.FormNameValidation); }
            if (!MainHelper.IsEmail(form.Email)) { ModelState.AddError("email", @Dictionary.FormEmailValidation); }
            if (MainHelper.IsEmpty(form.Enquiry)) { ModelState.AddError("enquiry", @Dictionary.FormCommentValidation); }
        }
        if (!IsPost || !ModelState.IsValid)
        {
        @Html.Raw(library.RemoveFirstParagraphTag(Model.FormText.ToString()))
        <form method="post" action="@Model.Url">
            <fieldset>
                <legend>@Parameter.subject </legend>
                <div class="clearfix @Library.If(!ModelState.IsValidField("name"), "error")">
                @Html.Label(@Dictionary.FormName, "name")
                <div class="input">@Html.TextBox("name", form.Name, new { @class = "xlarge" })
                @if (!ModelState.IsValidField("name"))
                { <span class="help-inline">@string.Join(". ", @ModelState["name"].Errors)</span> }
                </div>
                </div>
                <div class="clearfix">
                @Html.Label(@Dictionary.AddressName, "address1")
                <div class="input">@Html.TextBox("address1", form.Address1, new { @class = "xlarge" })</div>
                </div>
                <div class="clearfix">
                @Html.Label("Add 2", "address2", new { @class = "hide" })
                <div class="input">@Html.TextBox("address2", form.Address2, new { @class = "xlarge" })</div>
                </div>
                <div class="clearfix @Library.If(!ModelState.IsValidField("email"), "error")">
                @Html.Label(@Dictionary.FormEmail, "email")
                <div class="input">@Html.TextBox("email", form.Email, new { @type = "email", @class = "xlarge" })
                @if (!ModelState.IsValidField("email"))
                { <span class="help-inline">@string.Join(". ", @ModelState["email"].Errors)</span> }
                </div>
                </div>
                <div class="clearfix @Library.If(!ModelState.IsValidField("name"), "error")">
                @Html.Label(@Dictionary.FormComment, "enquiry")
                <div class="input">@Html.TextArea("enquiry", form.Enquiry, new { @rows = 5, @cols = 25, @class = "xlarge" })
                @if (!ModelState.IsValidField("enquiry"))
                { <span class="help-inline">@string.Join(". ", @ModelState["enquiry"].Errors)</span> }
                </div>
                </div>
                @Context.GetAntiForgeryHtml()
            </fieldset>
            <div class="actions">
                <button id="SubmitForm" type="submit" class="btn">@Dictionary.Send</button>  
            </div>
        </form>
        @Html.ValidationSummary(@Dictionary.FormValidationSummary, new { @class = "alert-message block-message error" })
        }
        else
        {
            var ok = SendForm(form, @Parameter.subject);
            if (!ok)
            {
                <div id="errorMailSettings">
                    @Model.ErrorMessage
                </div>
            }
            else
            {
                // Set Thankyou text from our contact node
                <div id="thankYou">
                    <h2>@Model.ThankYouHeaderText</h2>
                    @Model.ThankYouMessageText
                </div>
            }
        }
    }
    @functions 
    {
        public bool SendForm(dynamic form, string queryParam)
        {
            // Get the variables from the form and set them in strings
            string strName = Library.StripHtml(form.Name).ToString();
            string strAddressLine1 = Library.StripHtml(form.Address1).ToString();
            string strAddressLine2 = Library.StripHtml(form.Address2).ToString();
            string strEmailFrom = Library.StripHtml(form.Email).ToString();
            string strMessage = Library.StripHtml(form.Enquiry).ToString();
            // Lets set the values passed in from the Macro
            string strEmailTo = Model.EmailTo.ToString();
            string strEmailSubject = queryParam;
            var now = DateTime.Now;
            var strTime = String.Format("{0:HH:mm:ss}", now);
            var strDate = String.Format("{0:dd/MM/yyyy}", now);
            // Let's Replace the placeholders in the email message body
            var strEmailBody = new StringBuilder(Model.EmailBody.ToString());
            strEmailBody.Replace("[Name]", strName); // Find and Replace [Name]
            strEmailBody.Replace("[AddressLine1]", strAddressLine1); // Find and Replace [AddressLine1]
            strEmailBody.Replace("[AddressLine2]", strAddressLine2); // Find and Replace [AddressLine2]
            strEmailBody.Replace("[Email]", strEmailFrom); // Find and Replace [Email]
            strEmailBody.Replace("[Message]", strMessage); // Find and Replace [Message]
            strEmailBody.Replace("[Time]", strTime); // Find and Replace [Time]
            strEmailBody.Replace("[Date]", strDate); // Find and Replace [Date]
            // Now the email is sent out to the owner, lets send out an email
            // to let the user know we have recieved their email & will respond shortly
            string strEmailReplySubject = Model.EmailReplySubject.ToString();
            var strEmailReplyBody = new StringBuilder(Model.EmailReplyBody.ToString());
            strEmailReplyBody.Replace("[Name]", strName); // Find and Replace [Name]
            return MainHelper.TrySendMail(strEmailTo, strEmailSubject, strEmailBody.ToString()) && MainHelper.TrySendMail(strEmailFrom, strEmailReplySubject, strEmailReplyBody.ToString());
        }
    }

    Note: i have tried sending a hardcoded value to the function and this works fine.

    Could i perhaps render the parameter into a hidden field in the form and get it like 

    string strSubject = Library.StripHtml(form.Subject).ToString();

    If so, how? 

    please help... this is driving me nuts.

  • J 150 posts 489 karma points
    Jul 20, 2012 @ 12:42
    J
    0

    Hi Andreas,

    Did you add the Parameter in Umbraco?

  • Andreas Kristensen 20 posts 79 karma points
    Jul 20, 2012 @ 12:47
    Andreas Kristensen
    0

    Hi Jorge,

    Yes, i have added the parameter to the macro in umbraco as your screenshot shows.
    I use lowercase for alias and name though, could that have an effect? 

  • J 150 posts 489 karma points
    Jul 20, 2012 @ 13:10
    J
    0

    Hi Andreas,

    I don't think lowercasing the parameter would cause that problem. Are you using Visual Studio? If so, you should be able to debug the razor view.

    Anyway, there are two things you could try:

    1) Remove the at symbol in

    var ok = SendForm(form, Parameter.subject);

    or 2) Change the helper method SendForm to

    public bool SendForm(dynamic form)

    and add the parameter in the return

    return MainHelper.TrySendMail(strEmailTo, Parameter.subject, strEmailBody.ToString()) && MainHelper.TrySendMail(strEmailFrom, strEmailReplySubject, strEmailReplyBody.ToString());

    Hope this helps. J

  • Andreas Kristensen 20 posts 79 karma points
    Jul 20, 2012 @ 14:14
    Andreas Kristensen
    0

    Hi Jorge,

    I have tried both your suggestions but the result is the same, no value comes trough.

    I really need to have this working yesterday, couldent i just put the parameter into a hidden field and then read it when i call the function?
    I just tested it using the value of the name field and that worked fine... gonna go try. 

  • Andreas Kristensen 20 posts 79 karma points
    Jul 20, 2012 @ 16:20
    Andreas Kristensen
    0

    Thank Nonexistant God! 

    I finaly got it working by doing as i described above: rendering the value into the page and then stripping it back into the function as a parameter.

    Still its strange that i can't get a macro parameter into functions in razor...

    <div class="clearfix hide">
    <div class="input">
    <input class="xlarge" id="Subject" name="Subject" type="text" value="@Parameter.subject">
    </div>
    </div>

Please Sign in or register to post replies

Write your reply to:

Draft