Url.Action in jquery ajax: Second parameter not passed

安稳与你 提交于 2020-01-05 13:09:31

问题


I am trying to pass two parameters in an Url.Action function with jQuery ajax, and only the first parameter is passed. Both parameters show up correctly in the ajax function when I debug. The values passed in data are passed correctly.

What am I doing wrong?

Here is the code from the view:

<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>

And from the controller:

[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
}

回答1:


I guess the problem is with your final URL string. Try wrapping it in Html.Raw to prevent escaping & symbols:

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

Taken from this answer.



来源:https://stackoverflow.com/questions/25367062/url-action-in-jquery-ajax-second-parameter-not-passed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!