javascript - 如何在 ASP .NET 中将变量从 View 传递到 Controller

标签 javascript html asp.net asp.net-mvc

我发现了与我类似的问题,但在所有这些示例中,变量都是模型的一部分。我正在尝试传递一个在 javascript 中创建的变量,它不是模型的一部分。

代码:

 $(document).ready(function () {

    var url = document.URL;
    var index = url.indexOf("?email=");
    var email;

    /* If there is an EMAIL in URL, check directory to confirm it's valid   */
    if (index > -1) {
        /* There is an email */
        email = url.substr((index + 7));
        email = email.substr(0, (email.length - 4)) + "@@mymail.ca";

        /* Check directory to see if this email exists */
        @Html.Action("CheckDirectory", "Home", new { email = ???});

    }
});

有没有办法填写???使用上面的电子邮件?

最佳答案

您可以在 Controller URL 中将您的值作为 GET 参数传递:

$(document).ready(function () {

  var url = document.URL;
  var index = url.indexOf("?email=");
  var email;

  /* If there is an EMAIL in URL, check directory to confirm it's valid   */
  if (index > -1) {
    /* There is an email */
    email = url.substr((index + 7));
    email = email.substr(0, (email.length - 4)) + "@@mymail.ca";

    /* Check directory to see if this email exists */
    window.location.href = '/CheckDirectory/Home?email=' + email;
  }
});

关于javascript - 如何在 ASP .NET 中将变量从 View 传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32360431/

相关文章:

javascript - 需要显示所有选项是使用 jQuery 的基于字母的导航

javascript - 返回与类名匹配的所有元素

javascript - 在 Mocha 测试中找不到 Chai 导出

javascript - 如何向上滚动文本并覆盖 javascript/jquery 中的现有文本?

javascript - 如何根据位置和相应的href添加类?

javascript - Jquery按钮单击以从右到左显示带有幻灯片动画的div

JavaScript 变量问题

javascript - 在 ASP.NET 中动态创建自定义控件

asp.net - 如何授权在 Asp.Net MVC 中使用外部登录登录的用户

javascript - 如果浏览器在 asp .net 中关闭,请从浏览器注销?