Jquery 使用Ajax.ActionLink传递用户输入并设置url参数

Jquery 使用Ajax.ActionLink传递用户输入并设置url参数,jquery,ajax,asp.net-mvc,Jquery,Ajax,Asp.net Mvc,好吧,我只想传递用户输入,而不使用显式jqueryajax方法。我的想法如下:尝试使用Ajax.ActionLink并在Ajax本身之前为Ajax设置必要的参数: @Ajax.ActionLink("Call date", "CallingHistory", "Member", new {sortOrder = ViewBag.DateSortParam, pageNumber = ViewBag.PageNumber}, new AjaxOptions {UpdateTargetId = "h

好吧,我只想传递用户输入,而不使用显式jqueryajax方法。我的想法如下:尝试使用Ajax.ActionLink并在Ajax本身之前为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"})
和用于设置操作的Url参数的js代码:

<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>
在chrome web工具中出现错误:

获取localhost:50307/Views/Shared/Partial/Member/CallingHistory.cshtml?X-Requested-With=XMLHttpRequest&979;=1409287722659404(未找到)

脚本中的调试器显示resultUrl变量的此类值:

//localhost:50307/Member/CallingHistory?sortOrder=datepageNumber=1&includedialling=false&includeincomingmissedcall=true&includeoutcomingmissedcall=false。-一切都很好。但最终我还没有得到结果。此字符串“X-Requested-With=XMLHttpRequest&=1409287722659”从何处出现?显然,这是浏览器的行为


是..在您的问题中,您以正确的方式调用了
setUrlParameters
,但是您在
setUrlParameters
函数中使用了
this
,并且您没有提供该函数,而是以这种方式执行:

@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>
@Ajax.ActionLink(“调用日期”、“调用历史记录”、“成员”,新{sortOrder=
ViewBag.DateSortParam,pageNumber=ViewBag.pageNumber},新的AjaxOptions
{UpdateTargetId=“historytable”,HttpMethod=“get”,OnBegin=
“SetUrlParameters(this)”},新的{id=“calldatetimelink”})
函数setUrlParameters(数据){
var-link;
link=data.href+“&includedialling=“+$(“#chBox1”)。is(':checked')+“&includeincomingmissedcall=“+$(“#chBox2”)。is(':checked')+”&includeoutingmissedcall=“+$(“#chBox3”)。is(':checked');
$(“#calldatetimelink”).attr('href',link)
}

不要忘记在页面或布局页面中包含
'jquery.unobtrusive ajax.min.js'
文件。

谢谢,例外。我只是jquery.OnBegin js函数的新手,我看到url参数已设置,但在服务器端方法中获取空值。未传递参数-这种方式不起作用。)还有一个更常见的问题:我正在做一个表来对数据进行排序和分页,表头是ajax.action链接,服务器端方法(如上所述)有5个参数,前2个参数总是正确传递的。但是那些在OnBegin中设置的是空的。据我所知,传递用户输入的唯一方法是在没有Ajax.action链接的情况下重写?包含指向ajax.unobtrusive.js库的链接。@Rayan…我认为上面的答案应该有效…只需仔细检查拼写,而不是上面显示的“?”它应该是“&”。在javascript函数中创建完整的url后,只需检查并放置一个警告框。应该有效。。。
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;
}
@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>