2017-05-25 54 views
0

這個ajax從.cshtml文件調用到控制器,但getValue函數在加載時取值爲空,但之後工作正常。它平均顯示ajax調用上的警報值,但在控制器上變爲空值。任何幫助將不勝感激。函數在mvc5中取ajax的空值

<script> 
 
$(document).ready(function(){ 
 
    //get value from dropdown list 
 
    var ID = $('#cmb').val(); 
 
    // ID is taking value   
 
       $.ajax({ 
 
        // /ControllerName/FunctionName 
 
        // /Home/getvalue 
 
        url: "/Home/getValue", 
 
        type: "POST", 
 
        // ID is getting from dropdownList 
 
        // parameterName/value               
 
        data: JSON.stringify({'_ID':ID}), 
 
        dataType: "json", 
 
        traditional: true, 
 
        contentType: "application/json; charset=utf-8", 
 
        success: function (data) { 
 
         if (data.status == "success") { 
 
          //if got response      
 
          //alert(data.status); 
 
         } 
 
        }, 
 
        error: function() { 
 
         //if if not response 
 
         //alert('something went wrong'); 
 
        } 
 
       }); 
 
     }); 
 
</script>

[HttpPost] 
public JsonResult getValue(string _ID) 
{ 
    string a = _ID; 
} 
+0

數據: '{ 「_ID」:' + ' 「' + ID + '」}' –

+0

可能你沒有在ID值。或嘗試使用:data:{_ ID:ID} –

回答

0

這是合理的,你是在負載走空,因爲id爲CMD HTML元素的值將是不確定的,因爲我認爲有ISN」 t選擇的任何值(選擇元素的選項具有名爲selected的屬性,當您選擇一個值時,此屬性的值爲true)。

奇怪的是,你說事後按預期工作。我懷疑這一點,除非你有任何其他代碼沒有包含在你的文章中。因爲在DOM成功加載之後,似乎只有一個AJAX調用。

通常我們註冊一個事件,當我們這樣做一個AJAX調用。我可以假設你可以註冊到選擇元素的值(下拉列表)

$(document).ready(function(){ 
    $("#cmb").on("change", function(){ 
     //get value from dropdown list 
     var ID = $(this).val(); 
     // ID is taking value   
     $.ajax({ 
      // /ControllerName/FunctionName 
      // /Home/getvalue 
      url: "/Home/getValue", 
      type: "POST", 
      // ID is getting from dropdownList 
      // parameterName/value               
      data: JSON.stringify({'_ID':ID}), 
      dataType: "json", 
      traditional: true, 
      contentType: "application/json; charset=utf-8", 
      success: function (data) { 
       if (data.status == "success") { 
        //if got response      
        //alert(data.status); 
       } 
      }, 
      error: function() { 
       //if if not response 
       //alert('something went wrong'); 
      } 
     }); 
    }); 
}); 
0

你必須改變你的網址和參數檢查下面的代碼爲的onChange事件:

$(document).ready(function(){ 
    //get value from dropdown list 
    var ID = $('#cmb').val(); 
    // ID is taking value  
    $.ajax({ 
     url: "/Home/getValue?_ID=" + ID, 
     type: "POST", 
     dataType: "json", 
     data: 'json', 
     success: function (data) { 
      if (data.status == "success") { 
       //if got response      
       //alert(data.status); 
      } 
     }, 
     error: function() { 
      //if if not response 
      //alert('something went wrong'); 
     } 
    }); 

}); 

乾杯!

0
Change Paramaters Name 
use id in replace of _ID 

[HttpPost] 
public JsonResult getValue(string id) 
{ 
    string a = id; 
} 
$(document).ready(function(){ 
    $("#cmb").on("change", function(){ 
     //get value from dropdown list 
     var id= $(this).val(); 
     // ID is taking value   
     $.ajax({ 
      // /ControllerName/FunctionName 
      // /Home/getvalue 
      url: "/Home/getValue", 
      type: "POST", 
      // ID is getting from dropdownList 
      // parameterName/value               
      data: JSON.stringify({'id':ID}), 
      dataType: "json", 
      traditional: true, 
      contentType: "application/json; charset=utf-8", 
      success: function (data) { 
       if (data.status == "success") { 
        //if got response      
        //alert(data.status); 
       } 
      }, 
      error: function() { 
       //if if not response 
       //alert('something went wrong'); 
      } 
     }); 
    }); 
}); 
0

如果您確定您的ID不爲空,那就試試這個。

<script> 
$(document).ready(function(){ 
var ID = $('#cmb').val();  
      $.ajax({ 
       url: "/Home/getValue", 
       type: "POST",              
       data: {_ID:ID}, 
       dataType: "json", 
       contentType: "application/json; charset=utf-8", 
       success: function (data) { 
        if (data.status == "success") { 
         //if got response      
         //alert(data.status); 
        } 
       }, 
       error: function() { 
        //if if not response 
        //alert('something went wrong'); 
       } 
      }); 
    });