我是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;
}
}
这是否可以使用剃刀宏脚本。我有其余的模板设置
答案 0 :(得分: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};
}
}