[英]Why can't I bind form data directly to action method parameter?
I have action method like that: 我有这样的动作方法:
public ActionResult Index(HttpPostedFileBase image, int variable)
and form element like that: 并形成这样的元素:
variable 1:<input type="text" name="variable" value="1234" />
when I start debugging I am getting following exception: 当我开始调试时,出现以下异常:
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'.
参数字典在'Stream.Controllers.HomeController'中为方法'System.Web.Mvc.ActionResult Index(System.Web.HttpPostedFileBase,Int32)'的非空类型'System.Int32'的参数'variable'包含空条目”。 An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
可选参数必须是引用类型,可为空的类型,或者必须声明为可选参数。 Parameter name: parameters
参数名称:参数
what is wrong with this? 这有什么问题?
Try this: 尝试这个:
public ActionResult Index(HttpPostedFileBase image, int? variable)
The question mark ( ? ) indicates that variable is a nullable variable and then it can be assigned the null value. 问号(?)表示变量是可为空的变量,然后可以为其分配空值。
To know more about nullable types, read this on MSDN: Nullable Types (C# Programming Guide) 若要了解有关可空类型的更多信息,请在MSDN上阅读: 可空类型(C#编程指南)
Try: 尝试:
<input type="text" id="variable" name="variable" value="1234" />
Give an ID and see if you get the same issue... also, I know you probably know this, but to verify, you did put it within the form being posted, correct? 给出一个ID,看看是否遇到相同的问题...另外,我知道您可能知道这一点,但是要进行验证,您确实将它放在了要发布的表单中,对吗?
确保您的<input>
在<form>
标记中,并且该表单是正在执行其操作的表单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.