gpt4 book ai didi

c# - "The parameters dictionary contains a null entry..."错误

转载 作者:行者123 更新时间:2023-11-27 22:32:07 29 4
gpt4 key购买 nike

基本上我是一个 Winforms 的人。学习 ASP.Net 和 MVC 并陷入了一个问题。

我有一个名为 Calculation 的页面。内容如下

<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
<h2>About</h2>
<p>
<%: ViewData["Message"] %>
</p>
</asp:Content>

HomeController.cs

    public ActionResult Calculation(int value)
{
ViewData["Message"] = String.Format("{0} when squared produces {1}", value, Models.Mathematics.Square(value));
return View();
}

我按下 ctrl+5 并到达了我的主页。单击计算页面并收到错误信息

The parameters dictionary contains a null entry for parameter 'value' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Calculation(Int32)' in 'MathSolution.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

我试过了http://localhost:3670/Home/Calculation/5 还有http://localhost:3670/Home/Calculation?value=5。我仍然收到错误。我还尝试将 HttpPost 放在 HomeController.cs 中的 Calculation 方法上。

但是,如果在 HomeContoller.cs 文件计算方法中我为参数设置了默认值,则错误消失并且我始终获得默认值。

老实说,我的教程能够使它工作,但我不确定我的解决方案有什么问题。如果您需要更多信息,请告诉我。

最佳答案

最简单的解决方案,不一定是最好的:尝试将参数名称更改为“id”

public ActionResult Calculation(int id)
{
ViewData["Message"] = String.Format("{0} when squared produces {1}", id, Models.Mathematics.Square(id));
return View();
}

检查 this更准确的解释

关于c# - "The parameters dictionary contains a null entry..."错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16969889/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com