简体   繁体   English

为什么不能将表单数据直接绑定到操作方法参数?

[英]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.

相关问题 为什么我无法使用ajax将参数发送到动作方法? - Why I can't send parameter to action method with ajax? 在动作方法中,如何将发布数据绑定到动态对象? - In an action method, how can I bind post data to a dynamic object? WPF:为什么我不能绑定数据? - WPF: Why can't I bind data? 为什么我不能将url中的参数直接传递给MVC中的方法(参数始终为null) - why can I not pass a parameter in the url directly to my method in MVC (the parameter is always null) 无法访问Controller中带有参数的操作方法 - action method with parameter in Controller can't be accessed 为什么我不能将逆变接口作为接口上的方法的参数? - Why can't I take a contravariant interface as a parameter to a method on the interface? 如何通过Ajax POST将数据绑定到Action方法的两个复杂类型参数 - How can I bind data to two complex type parameters of Action method by Ajax POST 为什么我不能将表单数据发布到C#MVC4上的HttpPost方法中? - Why I can't post my Form Data to my HttpPost method on C# MVC4? 无法绑定到参数 - Can't bind to parameter 为什么我不能直接在方法上调用BeginInvoke,而不必将其分配给委托? - Why can't I call BeginInvoke directly on a method rather than having to assign it to a delegate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM