我正在尝试使用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
}
答案 0 :(得分:3)
我猜问题是你的最终网址字符串。尝试将其包装在Html.Raw中以防止转义&amp;符号:
...
url: '@Html.Raw(Url.Action("SendEmail", "Search", new { title = ViewData["title"], recordURL = ViewData["recordURL"] }))',
...
取自answer。