【发布时间】:2017-02-17 21:41:50
【问题描述】:
问题
我的注册页面上有一个链接,它需要调用控制器中发送代码的方法。当用户完成表单后点击提交,您可以检查代码是否匹配并注册用户。
我遇到的问题是当用户单击“发送代码”链接时,我需要传递已作为参数输入的电子邮件,而我似乎无法这样做。
代码
这是我的 ActionLink 的设置方式:
<%: Html.ActionLink("Send verification code", "Verify", new { id = "codeLink" })%>
此 ActionLink 对 Controller 中的方法进行 Javascript AJAX 调用以停止页面刷新。 变量“email”在我调试和测试后被填充。
$("#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"
});
});
邮箱的设置方式如下:
<p>
<%:Html.Label(Resources.UserEmailAddress)%>
<%:Html.TextBoxFor(m => m.uEmail, new { id = "uEmail" }) %>
<%:Html.ValidationMessageFor(m => m.uEmail) %>
</p>
这些步骤成功调用了控制器上的方法,但参数(JSON)没有被传递。
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);
}
所以我需要一种用户单击“发送代码”的方法,该方法停留在同一页面上并调用控制器中的验证方法,传递已在其上方输入的电子邮件。
【问题讨论】:
-
我假设您的
ActionLink()中有错字 - 您使用的 this overload 添加了路由值,而不是 html 属性(您没有使用<a id="codeLink" ...>生成元素 -
如果不是拼写错误,那么您只需进行正常重定向,而不为
email参数发送任何内容 -
@StephenMuecke 抱歉,我不确定您的意思。你能给我解释一下吗?
-
你应该使用简单的
<a id="codeLink">Click</a>。 @StephenMuecke 上面解释得更好, -
必须是
Html.ActionLink("Send verification code", "Verify", null, new { id = "codeLink" })才能在<a>标签中添加id="codeLink"
标签: javascript c# jquery ajax asp.net-mvc