Razor 联系人邮件中的动态主题

Razor 联系人邮件中的动态主题,razor,umbraco,Razor,Umbraco,我正在使用一个简单的联系人系统,在这个系统中,我需要在发送的电子邮件的主题字段中显示查询字符串中的值 今天是一个缓慢下降到疯狂的过程,因为我尝试过的每一种方法都失败了 我正在将Umbraco 4.7.2与Ubootstrap(twitter引导)一起使用,并修改其中的联系人表单。 下面是我的联系人表单宏的cshtml @using System.Text @using System.Collections.Generic; @using Bootstrap.Logic.Utils @using

我正在使用一个简单的联系人系统,在这个系统中,我需要在发送的电子邮件的主题字段中显示查询字符串中的值

今天是一个缓慢下降到疯狂的过程,因为我尝试过的每一种方法都失败了

我正在将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());
    }
}
@使用System.Text
@使用System.Collections.Generic;
@使用Bootstrap.Logic.Utils
@使用umbraco.macro引擎
@继承动态文本
@{
动态表单=Context.Request.form.ToDynamic();
如果(IsPost)
{
如果(!Context.isvalidatiforgery()){ModelState.AddFormError(@Dictionary.InvalidPost);}
if(mainheloper.IsEmpty(form.Name)){ModelState.AddError(“Name”,@Dictionary.FormNameValidation);}
如果(!mainheloper.IsEmail(form.Email)){ModelState.AddError(“Email”,@Dictionary.FormEmailValidation);}
if(mainheloper.IsEmpty(form.Enquiry)){ModelState.AddError(“Enquiry”,@Dictionary.FormCommentValidation);}
}
如果(!IsPost | |!ModelState.IsValid)
{
@Html.Raw(library.RemoveFirstParagraphTag(Model.FormText.ToString()))
@参数.subject
@Html.Label(@Dictionary.FormName,“name”)
@TextBox(“name”,form.name,new{@class=“xlarge”})
@如果(!ModelState.IsValidField(“名称”))
{@string.Join(“.”,@ModelState[“name”].Errors)}
@Html.Label(@Dictionary.AddressName,“address1”)
@TextBox(“address1”,form.address1,new{@class=“xlarge”})
@Label(“add2”,“address2”,new{@class=“hide”})
@TextBox(“address2”,form.address2,new{@class=“xlarge”})
@Html.Label(@Dictionary.FormEmail,“电子邮件”)
@TextBox(“email”、form.email、new{@type=“email”、@class=“xlarge”})
@如果(!ModelState.IsValidField(“电子邮件”))
{@string.Join(“.”,@ModelState[“email”].Errors)}
@Html.Label(@Dictionary.FormComment,“查询”)
@TextArea(“enquiry”,form.enquiry,new{@rows=5,@cols=25,@class=“xlarge”})
@如果(!ModelState.IsValidField(“查询”))
{@string.Join(“.”,@ModelState[“inquiry”].Errors)}
@Context.GetAntiForgeryHtml()
@字典,发送
@Html.ValidationSummary(@Dictionary.FormValidationSummary,new{@class=“警报消息阻止消息错误”})
}
其他的
{
var ok=SendForm(form,@Parameter.subject);
如果(!ok)
{
@Model.ErrorMessage
}
其他的
{
//从我们的联系人节点设置感谢文本
@Model.ThankYouHeaderText
@Model.ThankYouMessageText
}
}
}
@功能
{
公共bool SendForm(动态表单、字符串查询参数)
{
//从表单中获取变量并将其设置为字符串
字符串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();
字符串strMessage=Library.StripHtml(form.Enquiry.ToString();
//让我们设置从宏传入的值
字符串strEmailTo=Model.EmailTo.ToString();
字符串strEmailSubject=queryParam;
var now=DateTime.now;
var strTime=String.Format(“{0:HH:mm:ss}”,现在);
var strDate=String.Format(“{0:dd/MM/yyyy}”,现在);
//让我们替换电子邮件正文中的占位符
var strEmailBody=newstringbuilder(Model.EmailBody.ToString());
strEmailBody.Replace(“[Name]”,strName);//查找并替换[Name]
strEmailBody.Replace(“[AddressLine1]”,strAddressLine1);//查找并替换[AddressLine1]
strEmailBody.Replace(“[AddressLine2]”,strAddressLine2);//查找并替换[AddressLine2]
strEmailBody.Replace(“[Email]”,strEmailFrom);//查找并替换[Email]
strEmailBody.Replace(“[Message]”,strMessage);//查找并替换[Message]
strEmailBody.Replace(“[Time]”,strTime);//查找并替换[Time]
strEmailBody.Replace(“[Date]”,strDate);//查找并替换[Date]
//现在电子邮件已发送给所有者,让我们发送电子邮件
//为了让用户知道我们已经收到了他们的电子邮件,我们将很快回复
字符串strEmailReplySubject=Model.EmailReplySubject.ToString();
var strEmailReplyBody=新的StringBuilder(Model.EmailReplyBody.ToString());
strEmailReplyBody.Replace(“[Name]”,strName);//查找并替换[Name]
返回MainHelper.TrySendMail(strEmailTo、strEmailSubject、strEmailBody.ToString())和&MainHelper.TrySendMail(strEmailFrom、strEmailReplySubject、strEmailReplyBody.ToString());
}
}
(我知道代码墙,但我认为信息越多越好)

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


任何帮助都会很好。

函数上下文中不存在相应的参数。我建议将其作为参数传递给函数:

...

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

...

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

    string strEmailSubject = subject;

    ...
}

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

我知道@Parameter.subject允许我在表单中放置查询字符串,所以我在表单中创建了一个新的文本框来保存参数,然后将其剥离到