简体   繁体   中英

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32'

I keep getting this error passed to me when I submit an edit. I've tried a few different tactics like adding int? or sending the id's through the url, but I can't figure out what I'm doing wrong.

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32, Int32)' in 'XXX.Controllers.OBProfileTaskFieldsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

Parameter name: parameters

I have my controller that looks like this:

[HttpPost]
    public ActionResult Edit(int id, int tid, OBProfileTaskFields fieldToUpdate)
    {
        try
        {
            OBProfileTaskFields originalField = this.dbContext.OBProfileTaskFields.FirstOrDefault(obptf => obptf.ProfileID == fieldToUpdate.ProfileID && obptf.TaskID == fieldToUpdate.TaskID && obptf.FName == fieldToUpdate.FName);
            originalField.FRequired = fieldToUpdate.FRequired;
            originalField.FLocked = fieldToUpdate.FLocked;
            originalField.CCAccess = fieldToUpdate.CCAccess;
            originalField.EEAccess = fieldToUpdate.EEAccess;
            if (originalField.SeqNbr != fieldToUpdate.SeqNbr) ReorderProfileTaskFields(fieldToUpdate.ProfileID, fieldToUpdate.TaskID, fieldToUpdate.FName, (int)fieldToUpdate.SeqNbr);
            originalField.SeqNbr = fieldToUpdate.SeqNbr;
            this.dbContext.SaveChanges();
            return RedirectToAction("Index", "OBProfileTasks", new { id = originalField.ProfileID});
        }
        catch
        {
            return View(this.dbContext.OBProfileTaskFields.FirstOrDefault(obptf => obptf.ProfileID == fieldToUpdate.ProfileID && obptf.TaskID == fieldToUpdate.TaskID && obptf.FName == fieldToUpdate.FName));
        }
    }

and my view is like this:

<!--Modals-->
                    <div class="modal fade" id="instructions-@item.TaskID" tabindex="-1" role="dialog" aria-labelledby="instructionsLabel">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="instructionsLabel">Instructions</h4>
                                </div>
                                @using (Html.BeginForm("Edit", "OBProfileTaskFields", FormMethod.Post))
                                {
                                    <div class="modal-body">
                                        <input type="hidden" name="id" value="@item.ProfileID"/>
                                        <input type="hidden" name="tid" value="@item.TaskID" />
                                        <p>Placeholder text for isntructions or anything of that sort.</p>
                                        @Html.TextAreaFor(modelItem => item.CCInstruction, new {@class = "form-control", @rows = "6", @style = "width: 80%;"})
                                        <p>Placeholder text for isntructions or anything of that sort.</p>
                                        @Html.TextAreaFor(modelItem => item.EEInstruction, new {@class = "form-control", @rows = "6", @style = "width: 80%;"})
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                        <button type="submit" class="btn btn-primary">Save changes</button>
                                    </div>
                                }

                            </div>
                        </div>
                    </div>

I am passing the two different IDs that i assume I need.

You've got the error here: return RedirectToAction("Index", "OBProfileTasks", new { id = originalField.ProfileID}); You pass only 1 parameter while the error says that Index requires 2 ( Index(Int32, Int32) )

Here it's seems that the method Index required 2 parameters so you have to redirect with 2 paramters like`

  return RedirectToAction("Index", "OBProfileTasks", new { id = originalField.ProfileID, tid = 2});

Here we i choose tid= 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM