【问题标题】:MVC C# Pass parameter to VIA ActionLink to ControllerMVC C# 将参数传递给 VIA ActionLink 到控制器
【发布时间】: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 属性(您没有使用 &lt;a id="codeLink" ...&gt; 生成元素
  • 如果不是拼写错误,那么您只需进行正常重定向,而不为 email 参数发送任何内容
  • @StephenMuecke 抱歉,我不确定您的意思。你能给我解释一下吗?
  • 你应该使用简单的&lt;a id="codeLink"&gt;Click&lt;/a&gt;。 @StephenMuecke 上面解释得更好,
  • 必须是Html.ActionLink("Send verification code", "Verify", null, new { id = "codeLink" })才能在&lt;a&gt;标签中添加id="codeLink"

标签: javascript c# jquery ajax asp.net-mvc


【解决方案1】:

你必须传递一个 javascript object

var dataToPost = { email:email};
$.ajax({

        url: $(this).attr("href"),
        data: dataToPost,
        type: "POST",
        dataType: 'json',
        cache: false
 });

另外,contentType 是您发送的数据类型,所以application/json;默认为 application/x-www-form-urlencoded;字符集=UTF-8。

如果您使用application/json,则必须使用JSON.stringify() 才能发送JSON object

JSON.stringify() 将 javascript 对象转换为 json 文本并将其存储在字符串中。

var dataToPost = { email:email};
$.ajax({

        url: $(this).attr("href"),
        data: JSON.stringify(dataToPost),
        type: "POST",
        dataType: 'json',
        cache: false,
        contentType: "application/json; charset=utf-8"
 });

【讨论】:

  • 谢谢。如果他使用application/jsonrequest; charset=utf-8,他必须使用 JSON.stringify 来stringify javascript 对象。
  • 这不起作用。 “验证”方法应该接受什么参数?字符串还是对象?
  • 你删除了application/jsonrequest; charset=utf-8 吗?
  • 是的,我删除了那个位。
  • @BenClarke,看看我的回答。
猜你喜欢
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-07
  • 1970-01-01
  • 2010-09-14
相关资源
最近更新 更多