2014-08-28 96 views
1

那麼,我只想傳遞用戶輸入而不使用顯式的jQuery ajax方法。我的想法是以下幾點:試圖用Ajax.ActionLink併爲AJAX必要參數之前Ajax的本身:通過Ajax.ActionLink傳遞用戶輸入並設置url參數

@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代碼:

<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-方法之前Ajax的請求來執行。有可能這樣做嗎?我一直想把斷點設置爲js-method,但在chrome web-tools中看不到它。提前致謝。

UPDATE: 葉先生,覺得這應該工作,但是我改變的參數腳本事業無限的方式加:

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 & _ = 1409287722659 404(Not Found)

在調試器腳本顯示resultUrl變量的這樣的值:

//本地主機:50307 /會員/ CallingHistory中將sortOrder = datepageNumber = 1個& includedialling =假& includeincomingmissedcalls =真& includeoutcomingmissedcalls =假。 - 一切都很好。但最終我還沒有得到結果。這個字符串'X-Requested-With = XMLHttpRequest & _ = 1409287722659'從哪裏出現?顯然,這是瀏覽器的行爲。

回答

1

Yes..In你的問題,你在呼喚setUrlParameters以正確的方式,但使用的是thissetUrlParameters功能,你沒有提供它做的卻是這樣的:

@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'文件。

+0

謝謝,例外。我只是jquery的新手。 – Ryan 2014-08-28 09:38:39

+0

@ Rayan..problem解決.. ??? – 2014-08-28 10:11:36

+0

OnBegin js函數被觸發,我看到url參數已設置,但在服務器端方法獲取空值。 – Ryan 2014-08-29 02:39:15