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?
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 ajaxPostPath, string 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(a, b, e) {
throw e;
});
var v = $("#form").validate({
submitHandler: function(form) {
$(form).ajaxSubmit({
target: "#results",
url: "@ajaxPostPath",
data: { list_id: "@mailChimpListID"},
beforeSubmit: disableButton
});
}
});
});
</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(mailChimpListID, postedEmail, Request.UserHostAddress, String.Empty, String.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,
Can't you just add an int parameter to the SendSignUpForm() method so that you can pass in the ID?
Or am I not understanding correctly?
Oh yes, anyone can do that (including me), unless you have my girlfriend growling all night long!!!!!!!!!!!!
Thanks Dan
is working on a reply...