【问题标题】:Why do I get this exception when .NET try to execute a controller method?当 .NET 尝试执行控制器方法时,为什么会出现此异常?
【发布时间】:2014-07-03 16:08:57
【问题描述】:

我对 C# / ASP.NET MVC 框架很陌生,但遇到以下问题。

我有一个名为DeleteExecutableType.cshtml的视图:

@model DataModel.MaliciousCode.Malicious

@{
    ViewBag.Title = "DeleteExecutableType";
    Layout = "~/Views/Shared/MasterPageMobile.cshtml";
}

<h2>DeleteExecutableType</h2>

<h2>Malicious: @Model.Id</h2>
<h2>Fix: @Model.MaliciousCodeExecutableType[0].Title (id: @Model.MaliciousCodeExecutableType[0].Id)</h2>

<p>
    Confermare la cancellazione dell'Executable Type "@Model.MaliciousCodeExecutableType[0].Title" ?
</p>

@using (Html.BeginForm("DeleteExecutableType", "EditingMalicious", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    <input type="hidden" name="executableTypeId" value ="@Model.MaliciousCodeExecutableType[0].Id" />
    <input type="hidden" name="maliciousId" value ="@Model.Id" />


    <div data-role="controlgrup" data-type="horizontal" data-mini="true">
        <a href="@Url.Action("Edit", "Malicious", new { id = Model.Id })#tab-3" data-mini="true"  data-inline="true" data-role="button"  >Annulla</a>

        <input type="submit" value="Delete" data-mini="true" data-inline="true" />
    </div>
}

当用户单击Delete 按钮提交表单时,将调用EditingMaliciousController 类上定义的方法DeleteExecutableTypePost()

这里是:

[HttpPost, ActionName("DeleteExecutableType")]
[ValidateAntiForgeryToken]
public ActionResult DeleteExecutableTypePost(long maliciousId, long currentExecutableTypeId)
{
            if (maliciousId == null | currentExecutableTypeId == null)
            {
                return HttpNotFound();
            }

            manager.openConnection();
            try
            {
                manager.deleteSingleExecutableType(maliciousId, currentExecutableTypeId);
            }
            finally
            {
                manager.closeConnection();
            }

            return new RedirectResult(Url.Action("Edit", "Malicious", new { id = maliciousId }) + "#tab-2");
}

问题是当我尝试执行之前的方法时,我得到一个错误:

参数字典包含“MyWebApplication.Controllers.EditingMaliciousController”中方法“System.Web.Mvc.ActionResult DeleteExecutableTypePost(Int64, Int64)”的不可为空类型“System.Int64”的参数“currentExecutableTypeId”的空条目.可选参数必须是引用类型、可空类型或声明为可选参数。

参数名称:参数

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.ArgumentException:参数字典包含方法“System.Web.Mvc.ActionResult DeleteExecutableTypePost(Int64, Int64)”的不可空类型“System.Int64”的参数“currentExecutableTypeId”的空条目'MyWebApplication.Controllers.EditingMaliciousController'。可选参数必须是引用类型、可空类型或声明为可选参数。

参数名称:参数

为什么?我错过了什么?我该如何解决这个问题?

Tnx

【问题讨论】:

  • 恶意?你在做什么?不要这样做。
  • 花点时间阅读帮助中心的editing help。 Stack Overflow 上的格式设置与其他站点不同。您的帖子看起来越好,用户就越容易帮助您。对异常消息使用块引用 (&gt;)。

标签: c# asp.net-mvc


【解决方案1】:

是因为你没有input,textarea或者select名字为currentExceutableTypeId,所以不能从post collection绑定。

改变这个:

<input type="hidden" name="executableTypeId" value ="@Model.MaliciousCodeExecutableType[0].Id" />

到这里

<input type="hidden" name="currentExecutableTypeId" value ="@Model.MaliciousCodeExecutableType[0].Id" />

一切都应该很好

【讨论】:

  • 更好的方法是使用@Html.HiddenFor() 函数绑定隐藏变量,不必担心名称
【解决方案2】:

问题是你的表单域被称为executableTypeId,而action方法参数是currentExecutableTypeId。将这两个命名相同,你应该没问题。

【讨论】:

  • 完成了,但我还是有同样的问题
  • 检查表单是否实际发送回该字段中的值。您可以通过将参数类型设为long? 来消除异常,但这仅意味着您必须自己处理空值。
猜你喜欢
  • 2017-03-19
  • 2013-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多