0

我正在开发一个简单的联系系统,我需要在已发送电子邮件的主题字段中显示来自查询字符串的值。

今天已经慢慢陷入疯狂,因为这在我尝试过的每一种方式中都失败了。

我正在使用 Umbraco 4.7.2 和 Ubootstrap(通过 twitter 引导)并修改其中找到的联系表。下面是我的联系表格宏的cshtml

@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());
    }
}

(我知道代码墙,但我认为更多信息总比更少信息好)

这会导致通过图例字段中的@Parameter.subjet 显示来自宏参数的查询字符串,但是当我在发送函数中进一步尝试将字符串 strEmailSubject 设置为 @Parameter.subject 时,我只会在电子邮件中得到空字段.

任何帮助都会很棒。

4

2 回答 2

1

显然参数在函数上下文中不存在。我建议将它作为参数传递给函数:

...

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

...

public bool SendForm(dynamic form, string subject)
{
    ...

    string strEmailSubject = subject;

    ...
}
于 2012-07-18T17:30:10.857 回答
0

我设法通过以下方式解决了这个问题:

我知道@Parameter.subject 将允许我将查询字符串放入表单中,因此我在表单中创建了一个新文本框来保存参数,然后将其剥离 ala Library.StripHtml(form.Subject).ToString(); 并用它来调用 SendForm 函数

于 2012-07-20T18:02:02.170 回答