Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

javascript - The parameters dictionary contains a null entry for parameter 'wantedids' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.JsonResult'

In console showed me an error that parameters dictionary contains a null entry fo parameter wantedids, So I wanted to pass checked boxes to my controller using array, so only admin can check all boxes of tips for specific user, admin have more than 5 users, In my console i succesfully pass an checked elements but it only showed me an error after that Internal server error. How can I pass that so I can update my database with checked boxes, can someone help.

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

.js var wantedids = [];

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

Here is my 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;
        }
question from:https://stackoverflow.com/questions/65853939/the-parameters-dictionary-contains-a-null-entry-for-parameter-wantedids-of-non

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your wanted IDs seem to be a bool, but you're posting an array of values. What are the IDs data type? are they int? make it int[] wantedids

not convinced val() is the right jquery for a checkbox anyways. Do you mean to get the value from idtips?

And are you passing data for the other parameters in your ajax post data block?

Update:

You are only passing one parameter to the post:

data: { wantedids: wantedids},

However your method has more than one parameter

Statics(bool wantedids,int iduser,int idtips)

where do these others get set, since they are not nullable int? then they must be provided (unless they're set as a route parameter?)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...