【问题标题】:Getting null value to controller's action passing from javascript using jquery使用 jquery 获取从 javascript 传递的控制器操作的空值
【发布时间】:2012-02-08 14:37:11
【问题描述】:

我在我的项目中尝试的就像将复选框的选定值作为逗号分隔的字符串传递给我的控制器的 json.. 但我没有得到 json 操作的值.. 它在那里显示 null。

我该怎么做?请帮帮我

function getIds(checkList) 
{
    var idList = new Array();
    var loopCounter = 0;
    jQuery("input[name=" + checkList + "]:checked").each
    (
        function() 
        {
           idList[loopCounter] = jQuery(this).val();
           loopCounter += 1; 
        }
    );
       alert(idList);
          jQuery.getJSON("/Photos/CheckForIdsJson", { idList: idList });     
 }

 [AcceptVerbs(HttpVerbs.Get)]        
 public JsonResult CheckForIdsJson(Int32[] idList)
 {
       JsonResult result = new JsonResult();
        result.Data = idList;
         return result;
 }

【问题讨论】:

  • 你在哪里看到 null 值,在你的 js 文件中的 idList 中,在控制器代码中或在你的 js 代码中返回结果?
  • 也许你可以看看这个:stackoverflow.com/questions/536283/….
  • 在代码中添加一些警报以评估数组的状态,并使用 Firebug 评估代码以确定何时/何处丢失 idlist 中的值。一旦知道问题出在哪里,就可以专注于 JSON 方面。
  • 在我的控制器代码中......
  • uys,因为我所做的是删除多个选择复选框并将字符串传递给 json .....现在我这样做了.. – Labdhi Lakhani 3 分钟前编辑但我想要什么现在像在 json 方法(/Photos/CheckForIdsJson)中一样,我从 db 中删除 sme recrds 以及作为模型。bt 我无法将视图返回到同一控制器中的其他视图。我在删除其他名为“PhotoList”的视图后应该这样做调用以列出已完成更改的项目

标签: javascript jquery asp.net asp.net-mvc json


【解决方案1】:

你可以看看这个帖子:AJAX Post of JavaScript String Array to JsonResult as List<string> Always Returns Null? 或者这个帖子:Send list/array as parameter with jQuery getJson。看来您必须在 ajax 调用中指明 traditional: true

【讨论】:

  • 伙计们,我所做的是删除多个选择复选框并将字符串传递给 json .....现在我做到了..
  • 但是我现在想在 json 方法 (/Photos/CheckForIdsJson) 中做的事情我从 db 以及模型中删除 sme recrds .. bt 我无法将视图返回到同一控制器中的其他视图.. 我在删除其他名为“PhotoList”的视图后执行类似操作以列出已完成更改的项目..
  • 您是否尝试过return RedirectToAction("yourAction", "yourController"); 而不是返回结果?
【解决方案2】:

在你的脚本中使用它:

var did ='',
$("#tbl").find("input:checkbox").each(function (i) {
    if (this.checked == true) {
        did += $(this).val() + ",";
    }
});

alert(did); 

if (did == "") {
    alert("Please Select"); 
    return; 
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-30
    • 2017-10-19
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 2016-06-16
    • 2018-08-10
    • 1970-01-01
    相关资源
    最近更新 更多