[英]Url.Action Passing string array from View to Controller in MVC#
[英]Sending array from URL.Action and getting null in controller
我有一個 function,在 for 循環中添加項目后通過發送arr
參數(數組)的操作 URL 調用 controller,我看到它發送了數據,但 null 出現在 controller 參數中。 我想知道可能出了什么問題?
腳本 Function
function Submit() {
QRCodeval.forEach((code) => arr.push(code.innerHTML));
window.location.href = "@Url.Action("ReceivedFromBarCode","PurchaseOrder")?" +arr;
}
controller生成的鏈接:
http://localhost:56978/PurchaseOrder/ReceivedFromBarCode?JJJKP,RRPRQ,PPLQR
Controller 我在 arr 參數中得到 null。
public async Task<ActionResult> ReceivedFromBarCode(string[] arr)
{
return View();
}
MVC model 活頁夾期望 arrays 以以下格式傳遞: arr=value1&arr=value2&...
您可以使用map()
和join()
來創建正確的格式。 但是您還需要對這些值進行編碼,因為它們是 URL 中的參數encodeURIComponent
可以幫助解決這個問題:
let urlParams = array.map((val) => {
return `arr=${encodeURIComponent(val)}`
}).join('&');
window.location.href = "@Url.Action("ReceivedFromBarCode","PurchaseOrder")?"
+ urlParams;
和 append urlParams
到您的 URL。下面代碼段中的示例 output:
let array = ["value1", "http://example.com?x=1", "<script>alert('')</sctipt>"]; let urlParams = array.map((val) => { return `arr=${encodeURIComponent(val)}` }).join('&'); console.log(urlParams);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.