[英]Url action parameters using Ajax
我正在嘗試使用參數將數據從視圖傳遞到控制器。 現在我遇到了一些困難。 一旦我從表中選擇一行並按下對ShowTasks()具有onclick方法的按鈕,我就試圖傳遞這些參數
C#控制器:
[Route("/service/delivery/{id}/{shopdoccode}/{regdate}")]
public ActionResult Delivery(string id, string shopdoccode, string regdate)
{
//do stuf
}
用戶單擊按鈕時的Javascript函數:
function ShowTasks() {
//Dear Stackoverflow > This works, this is for selecting a row in the table
var $selectedRow = $(".highlight");
if ($selectedRow.length == 1) {
var dcColumn = 0;
var rdColumn = 1;
var shopdoccodeColumn = 3;
//assigning name to the colomn value
var id = $selectedRow[0].children[dcColumn].innerText.trim();
var regdate = $selectedRow[0].children[rdColumn].innerText.trim();
var shopdoccode = $selectedRow[0].children[shopdoccodeColumn].innerText.trim();
//ajax
if (id && regdate && shopdoccode) {
$.ajax({
type: 'POST',
url: '@Url.Action("service", "delivery" ,new { id = "id", shopdoccode = "shopdoccode", regdate = "regdate" })',
data: { id, regdate, shopdoccode },
success: function (data) {
if (data.success) {
console.log("Succes");
}
},
error: function (data) {
console.log("Error");
}
});
}
}
}
到目前為止,我做了什么? 坐了幾個小時,試圖找到一種將參數提供給控制器的方法,以便我可以調用SQL存儲過程。 不幸的是,我不能為此簡單地使用隱藏形式。
這也很有幫助: Url.Action參數?
在我看來,您的Url.Action
的參數順序錯誤。 更改為:
url: '@Url.Action("delivery", "service", new { id = "id", shopdoccode = "shopdoccode", regdate = "regdate" })',
這是您想要的適當的重載:
按此順序具有actionName,controllerName和routeValues的Action(字符串,字符串,對象) 。
使用Url.RouteUrl而不是Url.Action進行測試
您不能* .js或* .html文件寫剃刀代碼。
@Url.Action(string actionName,string controllerName,object routeValues)
上面的代碼只能用於* .cshtml文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.