jQuery ajax中的Url.Action:未传递第二个参数 [英] Url.Action in jquery ajax: Second parameter not passed
本文介绍了jQuery ajax中的Url.Action:未传递第二个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用jQuery ajax在Url.Action
函数中传递两个参数,并且仅传递第一个参数.当我调试时,两个参数都正确显示在ajax函数中. data
中传递的值正确传递.
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.
我在做什么错了?
这是视图中的代码:
<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
}
推荐答案
我想问题出在您的最终到达网址字符串.尝试将其包装在Html.Raw中,以防止转义&符号:
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"] }))',
...
来自此答案.
这篇关于jQuery ajax中的Url.Action:未传递第二个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文