2017-04-25 103 views
1

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

enter image description here

enter image description here

enter image description here

回答

1

你应该改变你邮寄到您的控制器的数据和Action你邮寄到:

data: { value = $CSV.text() } 

url: '@Url.Action("EditFence", "Configuration")' 

$CSV有可能涉及到HTML元素的jQuery对象。你需要阅读它的文本属性,并将其作为数据传递,而不是jQuery对象。

执行上述更改可以实现正确的POST。但是,关于您的控制器还有另一个问题。您的控制器在完成他的工作后不会响应AJAX调用,但会发出重定向。

更新

it would be helpful for you to tell me how the ActionResult should look, in terms of a return that doesn't leave the current view but rather just passes back that it was successful.

的动作,你POST需要重构像下面。正如你看到的,我们使用try/catch来捕获任何异常。如果没有发生任何异常,我们认为一切正常。否则,发生错误。在幸福的情况下,我们会返回带有成功消息的响应,而在不好的情况下,我们会返回带有失败消息的响应。

[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) 
    { 
     // log the Exception... 

     var obj = new 
     { 
      success = false, 
      responseText= "Zone save encountered a problem." 
     }; 
     return Json(obj, JsonRequestBehavior.AllowGet)); 
    } 
} 

这样做重构,你可以利用它在客户端如下:

$CSV.text(ConvertToCSV(JSON.stringify(data))); 

$.ajax({ 
    url: '@Url.Action("EditFence", "Configuration")', 
    type: "POST", 
    dataType: "json", 
    contentType: "application/json; charset=utf-8", 
    data: { value = JSON.stringify($CSV.text()) }, 
    success: function(response){ 
     alert(response.responseText); 
    }, 
    error: function(response){ 
     alert(response.responseText); 
    } 
}); 
+0

为什么?你能否提供OP的解释,以便他们知道为什么他们使用的数据传递方法没有与MVC action中的'value'参数相关联? – mhodges

+0

首先,他没有发布正确的行动。看起来好像。我期望通过改变这一点,它会看到他的行动受到打击。 – Christos

+0

我也假设'$ CSV'是一个DOM元素,我认为你做了相同的假设,但它有助于解释为什么传递一个DOM元素,甚至DOM元素的文本作为ajax调用的数据参数将没有链接到MVC操作参数 – mhodges

0

如果你的JavaScript实际上是一个JS文件,而不是CSHTML文件,那么这将发出一个字符串字面量:

@Url.Action("EditFile", "Configuration") 

HTML辅助不要在JS文件的工作......所以你需要指向一个实际的URL,像'/configuration/editfile'

此外,它看起来像张贴到一个名为EditFile的方法,但控制器代码片段中的方法名称是EditFence,所以这显然也是一个问题。

+0

它不在JS文件中,它在CSHTML文件中的脚本标记中。我想念我的问题。我的意思是EditFence,而不是EditFile。 –

0

你的问题是,在这些线路上:

success: alert("Zones have been saved."), 
error: alert("Zone save encountered a problem.") 

这将有效地立即同时运行的功能,并设置这些函数的返回值的成功和错误的性质。尝试使用匿名回调函数。

success: function() { 
    alert("Zones have been saved."); 
}, 
error: function() { 
    alert("Zone save encountered a problem.") 
} 
+0

这对我的两个问题都有帮助。 –

0

你不需要添加contentType默认application/x-www-form-urlencoded将工作,因为它看起来像你有一个大的CSV字符串。所以你的代码应该像这个例子

$(document).ready(function() { 

     // Create Object 
     var items = [ 
      { name: "Item 1", color: "Green", size: "X-Large" }, 
      { name: "Item 2", color: "Green", size: "X-Large" }, 
      { name: "Item 3", color: "Green", size: "X-Large" } 
     ]; 

     // Convert Object to JSON 
     var $CSV = $('#csv'); 
     $CSV.text(ConvertToCSV(JSON.stringify(items))); 
     $.ajax({ 
      url: '@Url.Action("EditFence", "Configuration")', 
      type: "POST", 
      dataType: "json", 
      data: {value:$CSV.text()}, 
      success: function(response) { 
       alert(response.responseText); 
      }, 
      error: function(response) { 
       alert(response.responseText); 
      } 
     }); 
相关问题