0

I'm new to umbraco. I'm trying to create a email widget to footer of my homepage. What i'm trying to achieve is sending a mail and then passing a json statuscode to javascript. I'm wondering whether is it possible to do this using Razor script

My Javascript code looks like follow

/* ---------------------------------------------------------------------- */
/* 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;
    });
}

My Razor script (Email.cshtml) so far look like this

@{

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;
} 

}

Is this possible with razor macro script. I have rest of the templates setup

Digbyswift
  • 10,310
  • 4
  • 38
  • 66
AllSpark
  • 425
  • 1
  • 4
  • 17

1 Answers1

0

I found the solution. Following is the Razor script. place this inside 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};
        }

    }
AllSpark
  • 425
  • 1
  • 4
  • 17