【问题标题】:Url.Action in jquery ajax: Second parameter not passedjquery ajax中的Url.Action:第二个参数未传递
【发布时间】:2014-10-11 14:07:04
【问题描述】:

我正在尝试使用 jQuery ajax 在 Url.Action 函数中传递两个参数,并且只传递了第一个参数。调试时,两个参数都正确显示在 ajax 函数中。 data 中传递的值是正确传递的。

我做错了什么?

这是视图中的代码:

<script type="text/javascript">

    $(function () {
        $("#sendForm").click(function () {
            //they both show correctly in the console
            console.log('@ViewData["recordURL"]');
            console.log('@ViewData["title"]');
            $.ajax({
                url: '@Url.Action("SendEmail", "Search", new { title = ViewData["title"], recordURL = ViewData["recordURL"] })',
                type: "POST",
                data: {
                    //placed these here so don't have to intersperse javascript with razor code in Url.Action
                    recepient: $("#recepient").val(),
                    message: $("#message").val()
                },
                success: function (result) {
                    $(".emailForm").hide();
                    $(".results").text(result);

                    window.setTimeout(closeEmailPopup, 3500);
                }
            });
        });
    });
</script>

从控制器:

[HttpPost]
public virtual ActionResult SendEmail(string title, string recordURL, string recepient, string message = "")
{
    // title is correct value, recordURL is null. 
    // If I switch the order in the URL.Action in the View,
    // then recordURL has correct value, and title is null 


    //recepient and message have correct value
}

【问题讨论】:

    标签: jquery ajax razor url.action


    【解决方案1】:

    我想问题出在您的最终 URL 字符串上。尝试将其包装在 Html.Raw 中以防止转义 & 符号:

    ...
    url: '@Html.Raw(Url.Action("SendEmail", "Search", new { title = ViewData["title"], recordURL = ViewData["recordURL"] }))',
    ...
    

    取自answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-07
      • 2011-06-30
      • 1970-01-01
      • 2014-05-02
      • 2021-09-29
      • 2019-10-26
      • 2015-06-18
      • 2018-11-07
      相关资源
      最近更新 更多