Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ioannis Gkoutsidis 16 posts 67 karma points
    Jan 30, 2012 @ 01:54
    Ioannis Gkoutsidis
    0

    Getting Macro Parameters from Razor Helper Method

    Hi all,

    trying to complete a simple form using JQuery, JQuery Validation Plugin, JQuery Form Plugin, Thomas Vestergaards MailChimp4Umbraco Plugin and Jonas Eriksson guidelines for Razor, I ended up with the following working code:

    @{
      string ajaxPostPath "/AjaxRazor?path=" VirtualPath;
      string mailChimpListID @Parameter.MailChimpListID
    }

    @(IsPost SendSignupForm(RenderSignupForm(@ajaxPostPath@mailChimpListID))

    @helper RenderSignupForm(string ajaxPostPathstring mailChimpListID)
    {
      <script type="text/javascript">
        function hideForm($("#form").hide()}
        function disableButton($(".submit").attr('disabled''disabled')}
        
        $(function({
          var loader $("#loader").hide();

          $(document).ajaxStart(function({
            hideForm();
            loader.show();
          }).ajaxStop(function({
            loader.hide();
          }).ajaxError(function(abe{
            throw e;
          });

          var $("#form").validate({
            submitHandlerfunction(form{
              $(form).ajaxSubmit({
                target"#results",
                url"@ajaxPostPath",
                datalist_id"@mailChimpListID"},
                beforeSubmitdisableButton
              });
            }
          });
        });
      </script>
      
      <form method="post" action="#" id="form">
        <input type="text" name="email" id="email" class="required email" />
        <br />
        <input type="submit" class="submit" value="Subscribe" />
      </form>
      <div id="loader" style="top: 5px; margin-right: 5px"><img src="/images/loading.gif" alt="loading..." width="20px" height="20px" />Loading...</div>
      <div id="results"></div>
    }

    @helper SendSignupForm()
    {
      string retval String.Empty;
      bool showResult;
      
      try
      {
        var postedEmail Request["email"];
        var mailChimpListID Request["list_id"];
        
        if (IsAjax)
        {
          // System.Threading.Thread.Sleep(5000);
          showResult true;
          retval "Invalid e-mail address";
          if (IsValidEmail(postedEmail))
          {
            showResult true;
            MailChimpCommunicator communicator new MailChimpCommunicator(ConfigurationManager.AppSettings["UmbracoMailChimp.APIKey"]);
            MailChimpResponse response communicator.AddSubscriber(mailChimpListIDpostedEmailRequest.UserHostAddressString.EmptyString.Empty);
            
            if (response.IsSuccess)
            {
              retval "Subscription completed";
            }
            else
            {
              string errorCode String.Format(" ({0})"response.ErrorCode);
              retval String.Format("Error {0}"errorCode);
            }
          }
        }
        else
        {
          showResult false;
          retval "Javascript disabled!";
        }
      }
      catch (Exception ex)
      {
        retval ex.Message;
      }
      finally
      {
       <div>@retval</div>
      }
    }

    @functions
    {
      private static bool IsValidEmail(string email)
      {
        bool retval false;
        if (!String.IsNullOrEmpty(email))
        {
          retval Regex.IsMatch(email@"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$"RegexOptions.IgnoreCase);
        }
        return retval;
      }
    }

    As you can see my programming skills are really poor, but this is the least concerned about right now!!

    My real issue is that this cshtml file is rendered via a macro with a "MailChimpListID" parameter, which I cannot access via my "SendSignupForm" helper method directly (i.e. using the Parameter directive).

    The only way I managed to get this working is using the form's "Data" property. Using this method though, leaves my List's ID unprotected, which I don't think is a good think...

    So, is there any way to get this value directly from my helper method?

    Thanks in advance,

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jan 30, 2012 @ 15:29
    Dan Diplo
    0

    Can't you just add an int parameter to the SendSignUpForm() method so that you can pass in the ID?

    @(IsPost ? SendSignupForm(@mailChimpListID) : RenderSignupForm(@ajaxPostPath, @mailChimpListID))
    
    @helper SendSignupForm(int mailChimpListID) { .... }

    Or am I not understanding correctly?

  • Ioannis Gkoutsidis 16 posts 67 karma points
    Jan 30, 2012 @ 16:13
    Ioannis Gkoutsidis
    0

    Oh yes, anyone can do that (including me), unless you have my girlfriend growling all night long!!!!!!!!!!!!

    Thanks Dan

Please Sign in or register to post replies

Write your reply to:

Draft