我在我的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