gpt4 book ai didi

c# - 为什么我不能将表单数据直接绑定(bind)到操作方法参数?

转载 作者:太空宇宙 更新时间:2023-11-03 14:27:46 24 4
gpt4 key购买 nike

我有这样的操作方法:

public ActionResult Index(HttpPostedFileBase image, int variable)

和这样的表单元素:

variable 1:<input type="text" name="variable" value="1234" />

当我开始调试时出现以下异常:

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

这是怎么回事?

最佳答案

试试这个:

public ActionResult Index(HttpPostedFileBase image, int? variable)

问号(?)表示该变量是可空变量,可以赋空值。

要了解有关可空类型的更多信息,请阅读 MSDN 上的这篇文章:Nullable Types (C# Programming Guide)

关于c# - 为什么我不能将表单数据直接绑定(bind)到操作方法参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3425442/

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