参数字典包含参数... mvc的空条目

时间:2017-08-13 06:05:57

标签: c# asp.net-mvc

我把我的视图和控制器放在这里,我的问题是在发布值后我得到了这个错误:

  

参数字典包含参数' id'的空条目。非可空类型的System.Int32' for method' System.Web.Mvc.ActionResult DeleteQuestionDo(Int32)' in' Landing.UI.Areas.cpanel.Controllers.BestDestinationResultController'。可选参数必须是引用类型,可空类型,或者声明为可选参数。   参数名称:参数

控制器:

[HttpPost]
public ActionResult DeleteQuestionDo(int id)
{
    Logic.BestDestination.BestDestinationLogic bestDestLigic = new Logic.BestDestination.BestDestinationLogic();
    bestDestLigic.deleteQuestion(id);
    return RedirectToAction("DeleteQuestion");
}

视图:

@model Landing.UI.Areas.cpanel.Models.BestDestinationQuestionList
....
@Html.BeginForm("DeleteQuestionDo", "BestDestinationResult", FormMethod.Post)
{
    @foreach (var m in Model.BestDestQuesList)
    {
        <input type="hidden" name="@m.QuestionId" />
        <br />
        <a href="@Url.Action("DeleteQuestionDo", "BestDestinationResult", new { id = m.QuestionId })">@m.Question</a>
        <input name="@m.QuestionId" type="submit"  value="حذف کامل سؤال"  />
    }
}

1 个答案:

答案 0 :(得分:2)

您的代码和逻辑存在多个问题,包括

  • 您方法中的参数名为# Keep the two columns "IlmnID" and "RefGene_Name" df <- df[, c("IlmnID", "RefGene_Name")] 但您没有生成 任何带有id的输入(它们被命名为name="id"1等 - 即 2属性的值,它不是有效的c#属性 无论如何名字)
  • 您的隐藏输入没有QuestionId属性,因此总是如此 post back null
  • 您生成多个链接,这些链接对一个方法进行GET 不存在
  • 您对value的使用不正确且无法生成 结束@Html.BeginForm(..)代码 - 需要</form>或者您需要包含@using (Html.BeginForm(...))

即使你确实添加了正确的名称和值属性,你也会在你的集合中回发一个包含所有Html.EndForm()值的数组,这是没有意义的(你只想删除一个)

您需要为集合中的每个项目生成QuestionId。在每个表单中,您可以添加<form>,但更容易将值添加为路由属性。

将您的代码更改为

<input type="hidden" name="id" value="@m.QuestionId" />

作为旁注,您使用@foreach (var m in Model.BestDestQuesList) { <div>@m.Question</div> @using (Html.BeginForm("DeleteQuestionDo", "BestDestinationResult", new { id = @m.QuestionId} )) { <input type="submit" value="حذف کامل سؤال" /> } } 表示您可能会重定向回相同的视图。如果是这种情况,您应该考虑使用ajax将return RedirectToAction("DeleteQuestion");值提交给&#39;删除&#39;方法(以及成功回调,从DOM中删除关联的元素),这将提高性能