【问题标题】:function is taking Null value by ajax in mvc5函数在 mvc5 中通过 ajax 获取 Null 值
【发布时间】:2017-05-25 05:07:45
【问题描述】:

这个 ajax 从 .cshtml 文件调用到控制器,但 getValue 函数在加载时采用 Null 值,但之后它工作正常。它甚至在 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;
 }

【问题讨论】:

  • 数据:'{"_ID": ' + '"' + ID + '"}'
  • 可能您的 ID 没有价值。或尝试使用:data:{_ID : ID}

标签: c# asp.net-mvc asp.net-ajax asp.net-mvc-5


【解决方案1】:

加载时使用 null 是合理的,因为带有 id cmd 的 HTML 元素的值是未定义的,因为我假设没有选择任何值(选择元素的选项有一个属性称为 selected,当您选择了一个值时,此属性的值为 true)。

奇怪的是,你说之后按预期工作。我对此表示怀疑,除非您有任何其他代码未包含在您的帖子中。似乎在成功加载 DOM 后只有一个 AJAX 调用。

当我们像这样进行 AJAX 调用时,通常我们会注册到一个事件。我可以假设您可以注册到选择元素值的 onChange 事件(下拉)

$(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');
            }
        });
    });
});

【讨论】:

    【解决方案2】:

    你必须改变你的 URL 和参数检查下面的代码:

    $(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');
            }
        });
    
    });
    

    干杯!!

    【讨论】:

      【解决方案3】:
      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');
                  }
              });
          });
      });
      

      【讨论】:

        【解决方案4】:

        如果你确定你的 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');
                        }
                    });
           });
        

        【讨论】:

          猜你喜欢
          • 2020-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-02
          • 1970-01-01
          • 1970-01-01
          • 2015-04-13
          • 1970-01-01
          相关资源
          最近更新 更多