参数字典包含参数';id';不可为null的类型';System.Int32';

本文关键字:参数 类型 Int32 null System 字典 包含 id | 更新日期: 2023-09-27 17:59:08

当我提交编辑时,我不断收到这个错误。我尝试了一些不同的策略,比如添加int?或通过url发送id,但我不知道自己做错了什么。

参数字典包含"XXX.Controller.OBProfileTaskFieldsController"中方法"System.Web.Mvc.ActionResult Index(Int32,Int32)"的不可为null类型"System.Int32"的参数"id"的null条目。可选参数必须是引用类型、可为null的类型,或者声明为可选参数。

参数名称:参数

我的控制器看起来像这样:

[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));
        }
    }

我的观点是这样的:

<!--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>

我正在传递我认为需要的两个不同的ID。

参数字典包含参数';id';不可为null的类型';System.Int32';

此处出现错误:return RedirectToAction("Index", "OBProfileTasks", new { id = originalField.ProfileID});您只传递1个参数,而错误显示Index需要2(Index(Int32, Int32)

这里的方法Index似乎需要2个参数,所以你必须用2个参数重定向,比如`

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

在这里,我们选择tid=2