1

我正在尝试Url.Action使用 jQuery ajax 在函数中传递两个参数,并且只传递了第一个参数。调试时,这两个参数都正确显示在 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
}
4

1 回答 1

3

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

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

取自这个答案

于 2014-08-18T16:07:23.493 回答