【问题标题】:The parameters dictionary contains a null entry for parameter 'wantedids' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.JsonResult'参数字典包含方法“System.Web.Mvc.JsonResult”的不可空类型“System.Int32”的参数“wantedids”的空条目
【发布时间】:2021-01-22 23:05:20
【问题描述】:

在控制台中向我显示参数字典包含参数wantedids 的空条目的错误, 所以我想使用数组将复选框传递给我的控制器,所以只有管理员可以检查特定用户的所有提示框,管理员有超过 5 个用户,在我的控制台中我成功传递了一个选中的元素,但它只显示了一个错误之后那Internal server error。我怎样才能通过它,以便我可以使用复选框更新我的数据库,有人可以帮忙吗?

    <input type="checkbox" class="cktips" idtips="@item.idtips 
    checked="@(item.iduser == ViewBag.iduser ? true : false)"/>

.js var Wantids = [];

    $("#btnClick").click(function () {
        $(".cktips").each(function () {
         $(this).prop('checked', true);
         ids.push($(this).val());
        });

        $.ajax({
        url: UrlSettingsDocument.Tips,
        data: { ids: ids},
        type: "POST",
        success: function () {
        alert('successs');
        },
        error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
       }
     })
    })

这是我的 Controller.cs

public JsonResult Statics(bool ids,int iduser,int idtips)
    {
        try
            {
                if (ids)
                {
                    statics = new statics ();
                    st.idtips= idtips;
                    Database.statics .Add(st);
                    Database.SaveChanges();
                }
            else if (!ids)
            {
                var stdelete= Database.statics.Where(a => a.iduser == iduser &&
                a.idtips== idtips).FirstOrDefault();
                Database.statics.Remove(stdelete);
                Database.SaveChanges();
            }
            if (Request.IsAjaxRequest())
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
        }
        catch (Exception ex)
        {
            Logger.Error("Error: {0}", ex.ToString());
            return null;
        }

【问题讨论】:

  • 嗨@Malinovski99,哪一行出现这样的错误?你成功传递到后端了吗?

标签: javascript c# asp.net-core


【解决方案1】:

您想要的 ID 似乎是一个布尔值,但您发布的是一组值。 IDs 数据类型是什么?他们是 int 吗?让它int[] wantedids

不相信val() 无论如何都是复选框的正确 jquery。您的意思是从 idtips 中获取价值吗?

您是否正在为您的 ajax post 数据块中的其他参数传递数据?

更新:

您只向帖子传递了一个参数:

data: { wantedids: wantedids},

但是你的方法有多个参数

Statics(bool wantedids,int iduser,int idtips)

这些其他的在哪里设置,因为它们不可为空 int? 那么必须提供它们(除非它们被设置为路由参数?)

【讨论】:

  • IDs 数据是 int 类型,我之前用 int[] wantedids 尝试过,你对 ajax post 中的其他参数是什么意思?
  • 你的控制器方法统计需要 3 个参数。你只通过一个。你从哪里传递 iduser 和 idtips?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
  • 1970-01-01
  • 2013-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多