好吧,我想要的是在不使用显式jquery ajax方法的情况下传递用户输入。我的想法如下:尝试使用Ajax.ActionLink并在ajax -self之前为ajax设置必要的参数:
@Ajax.ActionLink("Call date", "CallingHistory", "Member", new {sortOrder = ViewBag.DateSortParam, pageNumber = ViewBag.PageNumber}, new AjaxOptions {UpdateTargetId = "historytable", HttpMethod = "get", OnBegin = "SetUrlParameters"}, new {id = "calldatetimelink"})
和js-code,用于设置Action的Url参数:
<script>
function setUrlParameters() {
var k = 0;
this.href = this.href + "?includedialling=" + $("#chBox1").val();
this.href = this.href + "?includeincomingmissedcalls=" + $("#chBox2").val();
this.href = this.href + "?includeoutcomingmissedcalls=" + $("#chBox3").val();
}
</script>
动作方法签名如下所示:
public Task<ActionResult> CallingHistory(string sortOrder, int pageNumber, bool? includedialling,
bool? includeincomingmissedcalls, bool? includeoutcomingmissedcalls){...}
请注意,js-method在ajax-request之前执行。有可能吗?我想把断点设置为js-method但却无法在chrome web-tools中看到它。提前谢谢。
更新 是的,认为这应该工作,但我改变了脚本的参数原因是以无限的方式添加:
function setUrlParameters(data) {
var oldref = data.href;
var par1 = '&includedialling=' + $("#chBox1").is(":checked");
var par2 = '&includeincomingmissedcalls=' + $("#chBox2").is(":checked");
var par3 = '&includeoutcomingmissedcalls=' + $("#chBox3").is(":checked");
var arr = data.href.split('&');
var resultUrl = arr[0] + arr[1] + par1 + par2 + par3;
$("#calldatetimelink").href = resultUrl;
}
在Chrome网络工具中出错:
GET localhost:50307 / Views / Shared / Partial / Member / CallingHistory.cshtml?X-Requested-With = XMLHttpRequest&amp; _ = 1409287722659 404(Not Found)
脚本中的调试器显示了resultUrl变量的值:
//本地主机:50307 /会员/ CallingHistory中将sortOrder = datepageNumber = 1&安培; includedialling =假安培; includeincomingmissedcalls =真安培; includeoutcomingmissedcalls =假?。 - 一切都很好。但最终我还没有得到结果。这个字符串在哪里&#39; X-Requested-With = XMLHttpRequest&amp; _ = 1409287722659&#39;出现?显然,这是浏览器的行为。
答案 0 :(得分:1)
是..在您的问题中,您正在以正确的方式致电setUrlParameters
,但您正在使用this
setUrlParameters
内的@Ajax.ActionLink("Call date", "CallingHistory", "Member", new {sortOrder =
ViewBag.DateSortParam, pageNumber = ViewBag.PageNumber}, new AjaxOptions
{UpdateTargetId = "historytable", HttpMethod = "get", OnBegin =
"SetUrlParameters(this)"}, new {id = "calldatetimelink"})
<script>
function setUrlParameters(data) {
var link;
link = data.href + "&includedialling=" + $("#chBox1").is(':checked') + "&includeincomingmissedcalls=" + $("#chBox2").is(':checked') + "&includeoutcomingmissedcalls=" + $("#chBox3").is(':checked');
$("#calldatetimelink").attr('href',link)
}
</script>
功能,而您并未以这种方式提供它:
'jquery.unobtrusive-ajax.min.js'
并且不要忘记在您的网页或布局页面中加入{{1}}文件。