我在我的JavaScript中有一個AJAX函數來調用我的控制器方法。當我運行AJAX函數(在按鈕上單擊)時,它不會在我的方法中打破我的斷點。這一切都運行成功:和錯誤:。我需要更改哪些內容才能將值從$ CSV.text實際發送到我的控制器方法?Ajax不會調用MVC控制器方法
JAVASCRIPT:
// Convert JSON to CSV & Display CSV
$CSV.text(ConvertToCSV(JSON.stringify(data)));
$.ajax({
url: '@Url.Action("EditFence", "Configuration")',
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: { value : $CSV.text() },
success: function(response){
alert(response.responseText);
},
error: function(response){
alert(response.responseText);
}
});
CONTROLLER:
[HttpPost]
public ActionResult EditFence(string value)
{
try
{
WriteNewFenceFile(value);
Response.StatusCode = (int)HttpStatusCode.OK;
var obj = new
{
success = true,
responseText = "Zones have been saved."
};
return Json(obj, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
var obj = new
{
success = false,
responseText = "Zone save encountered a problem."
};
return Json(obj, JsonRequestBehavior.AllowGet);
}
}
RESULT
爲什麼?你能否提供OP的解釋,以便他們知道爲什麼他們使用的數據傳遞方法沒有與MVC action中的'value'參數相關聯? – mhodges
首先,他沒有發佈正確的行動。看起來好像。我期望通過改變這一點,它會看到他的行動受到打擊。 – Christos
我也假設'$ CSV'是一個DOM元素,我認爲你做了相同的假設,但它有助於解釋爲什麼傳遞一個DOM元素,甚至DOM元素的文本作爲ajax調用的數據參數將沒有鏈接到MVC操作參數 – mhodges