参数字典包含参数' id'的空条目。非可空类型的System.Int32'

时间:2015-06-23 17:37:33

标签: c# .net asp.net-mvc dictionary

当我提交编辑时,我不断将此错误传递给我。我尝试了一些不同的策略,例如添加int?或通过网址发送ID,但我无法弄清楚我做错了什么。

  

参数字典包含参数' id'的空条目。非可空类型的System.Int32' for method' System.Web.Mvc.ActionResult Index(Int32,Int32)'在' XXX.Controllers.OBProfileTaskFieldsController'。可选参数必须是引用类型,可空类型,或者声明为可选参数。

     

参数名称:参数

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

[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。

2 个答案:

答案 0 :(得分:1)

您在此处收到错误:return RedirectToAction("Index", "OBProfileTasks", new { id = originalField.ProfileID}); 只传递1个参数,而错误表明索引需要2(Index(Int32, Int32)

答案 1 :(得分:1)

这里似乎方法索引需要2个参数,所以你必须重定向2个参数,如`

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

我在这里选择tid = 2