Issue
I have a link on my Register page which needs to call a method in the Controller that sends out a code. When the user finishes the form they click submit and you can check the codes match and register the user.
The problem I am having is when the user clicks the Send Code link, I need to pass the email that has been entered as a parameter which i can't seem to do.
Code
Here is how my ActionLink is setup:
<%: Html.ActionLink("Send verification code", "Verify", new { id = "codeLink" })%>
This ActionLink makes a Javascript AJAX call to the method in the Controller to stop the page refreshing. The variable 'email' is populated as i have debugged and tested this.
$("#codeLink").click(function (e) {
e.preventDefault();
var dataToPost = "{ email:'" + email + "'}";
$.ajax({
url: $(this).attr("href"),
data: dataToPost,
type: "POST",
dataType: 'json',
cache: false,
contentType: "application/jsonrequest; charset=utf-8"
});
});
The way the email box is setup is like below:
<p>
<%:Html.Label(Resources.UserEmailAddress)%>
<%:Html.TextBoxFor(m => m.uEmail, new { id = "uEmail" }) %>
<%:Html.ValidationMessageFor(m => m.uEmail) %>
</p>
These steps successfully call the method on the controller but the parameter (JSON) does not get passed.
public void Verify(string email)
{
var VerificationCode = Guid.NewGuid().ToString();
VerificationCode = VerificationCode.Substring(VerificationCode.IndexOf("-") + 1, 4);
Session["VerificationCode"] = VerificationCode;
var emailUsername = new EmailValidationCode();
emailUsername.SendEmail(email, VerificationCode);
}
So I need a way of the user clicking "Send Code" which stays on the same page and calls the Verify method in the controller, passing the email that has been entered above it.