2014-03-28 20 views
1

我發佈車輛的id從控制器從jquery ajax post.Ajax調用正在調用控制器方法,但該字符串始終爲null.I在網上搜索了很多,但任何解決方案都不是working.id在視圖中有價值,但在controller.please中無效幫助我。jquery ajax調用控制器是張貼空字符串

這裏是我的代碼: 查看

  <script type="text/javascript"> 
      function DeleteVehicle(id) { 
      alert("working"); 
      if (confirm("Do you want to delete vehicle: " + id)) { 
     // var requestData = { 'vehicleId': vid } 
     var dataPost={'id':id} 
     $.ajax({ 



      url: '@Url.Action("DeleteVehicle", "Vehicle")', 
      type: 'POST', 
      data: dataPost, 
      success: function (data) { 
       alert("Success, sent data to controller"); 
      }, 
      error: function (data) { 
        alert("Error: " + data.responseText); 
       } 

     }); 
    } 
} 

$(function() {//when the document is ready 
    $("#Delete").click(DeleteVehicle); 
}); 

控制器

 public ActionResult DeleteVehicle(string id) 
     { 

      //code 
       return Json(id, JsonRequestBehavior.AllowGet); 
     } 
+0

在刪除點擊功能不通過車輛編號 – 111

+0

如果我不通過車輛編號控制器刪除功能我怎麼能得到它?我必須用它來查詢 – Wasfa

回答

2

有你把httppost在下面的操作方法的屬性?

[httpPost] 
public ActionResult DeleteVehicle(string id) 
     { 

      //code 
       return Json(id, JsonRequestBehavior.AllowGet); 
     } 

和jQuery的

$(document).ready(function() { 
     $("#Delete").on("click", function(){ 
     var id = $(this).).attr("id"); ////fetch id wherever you are fetching 
     alert("working"); 
       if (confirm("Do you want to delete vehicle: " + id)) { 
      // var requestData = { 'vehicleId': vid } 
      var dataPost={'id':id} 
      $.ajax({ 



       url: '@Url.Action("DeleteVehicle", "Vehicle")', 
       type: 'POST', 
       data: dataPost, 
       success: function (data) { 
        alert("Success, sent data to controller"); 
       }, 
       error: function (data) { 
         alert("Error: " + data.responseText); 
        } 

      }); 
     } 


    }); 

更多信息,看看這個

MVC Deleting record using Javascript pop up

+0

是的,我把[httpPost]動詞從刪除方法 – Wasfa

+0

你從哪裏獲得ID?因爲你的代碼你沒有傳遞id到函數「DeleteVehicle」@Wasfa – Neel

+0

我通過jquery ajax調用發佈的id,我在控制器參數 – Wasfa

1

var dataPost應該是字符串,像var dataPost = '{"id":"' + id + '"}'$.ajax額外設置如下 - >

contentType: "application/json; charset=utf-8", 
dataType: "json", 
+0

我試過這一個,但再次id字符串爲空在控制器 – Wasfa

0

試圖把[HttpPost]屬性在你的行動,然後再試一次。

+0

我嘗試這一個,但字符串ID爲null – Wasfa

+0

在你Ajax調用add'contentType:「application/json」'aftr'datapost' – 2014-03-28 07:17:23

+0

我加了這個但id仍然爲空 – Wasfa

相關問題