ASP.NET MVC2 Post 按钮具有正确的值,但不将值发送到操作
我有一个按钮,其代码是这样生成的:
<% RouteValueDictionary dictionary2 = new RouteValueDictionary(); %>
<% dictionary2.Add("EventID",0); %>
<% dictionary2.Add("CustomerID",Model.customer.CustomerID.ToString()); %>
<% using (Html.BeginForm("EventEdit", "Customers", dictionary2,FormMethod.Get,null ))
{ %>
<button type="submit">
new event</button>
<%} %>
实际生成的代码:
<form action="/Customers/EventEdit?EventID=0&CustomerID=1" method="get">
<button type="submit">
new event</button>
</form>
但按钮调用此地址:
我得到:
参数字典包含一个 参数“EventID”的条目为空 不可为 null 的类型“System.Int32” 方法'System.Web.Mvc.ActionResult 事件编辑(Int32,Int32)'中 'TechRun.UI.Controllers.CustomersController'。 可选参数必须是 引用类型、可为 null 的类型,或者是 声明为可选参数。 参数名称:参数
任何想法我做错了什么(该表单上还有其他发布按钮,但它们工作正常)。 谢谢。
I have a button who's code is generated like this:
<% RouteValueDictionary dictionary2 = new RouteValueDictionary(); %>
<% dictionary2.Add("EventID",0); %>
<% dictionary2.Add("CustomerID",Model.customer.CustomerID.ToString()); %>
<% using (Html.BeginForm("EventEdit", "Customers", dictionary2,FormMethod.Get,null ))
{ %>
<button type="submit">
new event</button>
<%} %>
The actual code generated:
<form action="/Customers/EventEdit?EventID=0&CustomerID=1" method="get">
<button type="submit">
new event</button>
</form>
But the buttons calls this address:
and I get:
The parameters dictionary contains a
null entry for parameter 'EventID' of
non-nullable type 'System.Int32' for
method 'System.Web.Mvc.ActionResult
EventEdit(Int32, Int32)' in
'TechRun.UI.Controllers.CustomersController'.
An optional parameter must be a
reference type, a nullable type, or be
declared as an optional parameter.
Parameter name: parameters
Any Idea what am I doing wrong (there are other post buttons on that form, but they work ok).
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这样:另外确保没有任何 javascript 可能会干扰发送 AJAX 请求的表单提交并忘记包含值。最后确保 Model.customer.CustomerID 不为空。使用 FireBug 准确查看发送到服务器的请求是什么。更新:
根据 规范:
这意味着您不应在带有 GET 方法的表单操作中使用查询字符串参数。您需要使用表单内的隐藏字段将这些值传递到服务器。
Try like this:Also make sure that there's no some javascript that could interfere with the form submission sending an AJAX request and forgetting to include the values. And finally make sure thatModel.customer.CustomerID
is not empty. Use FireBug to see exactly what is the request being sent to the server.UPDATE:
According to the specification:
This means that you should not use query string parameter in the form action with GET method. You need to use hidden fields inside the form to pass those values to the server.