0

我是 umbraco 的新手。我正在尝试在我的主页页脚创建一个电子邮件小部件。我想要实现的是发送邮件,然后将 json 状态码传递给 javascript。我想知道是否可以使用 Razor 脚本执行此操作

我的 Javascript 代码如下所示

/* ---------------------------------------------------------------------- */
/* Newsletter Subscription
/* ---------------------------------------------------------------------- */

if ($().validate) {
    $("#send-newsletter-form").validate();
}

var newsletterForm = $("#newsletter-form");
if (newsletterForm && newsletterForm.length > 0) {
    var newsletterSubscribeButton = newsletterForm.find("#subscribe");
    var newsletterEmailInput = newsletterForm.find("#newsletter");

    newsletterSubscribeButton.bind("click", function () {

        if ($("#newsletter-form").valid()) {
            $("#subscribe").attr('disabled', 'disabled');
            jQuery.ajax({
                type:"POST",
                url:\MacroScripts\Email.cshtml",
                data:getSubscribeFormData(),
                statusCode:{
                    200:function () {
                        $("#newsletter-notification-box-success").css('display', '');
                        newsletterSubscribeButton.removeAttr('disabled', '');
                        resetSubscribeFormData();
                    },
                    500:function () {
                        $("#newsletter-notification-box-error").css('display', '');
                        newsletterSubscribeButton.removeAttr('disabled');
                    }
                }
            });
        }

        function getSubscribeFormData() {
            var data = 'action=subscribe';
            if (newsletterEmailInput && newsletterEmailInput.length > 0) {
                data += '&email=' + newsletterEmailInput.attr('value');
            }
            return data;
        }

        function resetSubscribeFormData() {
            if (newsletterEmailInput && newsletterEmailInput.length > 0) {
                newsletterEmailInput.attr('value', '');
            }
        }

        return false;
    });
}

到目前为止,我的 Razor 脚本(Email.cshtml)看起来像这样

@{

var addressFrom = Request["email"];
const string addressTo = "myaddress@domain.com";

try
{
   umbraco.library.SendMail(addressFrom, addressTo, "SUBSCRIBE", "Email DUmmy Body", false);
    //Here i wanna return json statuscode of 200  to javascript 
}
catch (Exception)
{
    //Here i wanna return json statucode of 500 to javascript possibly with what went wrong.
    throw;
} 

}

这可能与剃刀宏脚本。我有其余的模板设置

4

1 回答 1

0

我找到了解决方案。以下是 Razor 脚本。将其放在 CHTML 中

 @using System.Web.Mvc
    @{
        SubscribeViaEmail();
    }

    @functions
    {
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult SubscribeViaEmail()
        {
            var response = Response;
            var flag = true;
            if(flag)
                response.StatusCode = (int)HttpStatusCode.InternalServerError;
            else
            {
                response.StatusCode = (int)HttpStatusCode.OK;
            }
            return new JsonResult(){Data = response};
        }

    }
于 2013-10-09T08:14:37.983 回答