Asp.net MVC Form Post to Action 缺少参数初始化
我在将 ASP.net MVC 表单发布到包含多个参数的操作时遇到问题。问题是只有部分参数被初始化。
该页面有多个表单,每个表单都由单独的控制器支持。
接收 Post 操作的控制器操作如下所示
public ActionResult Create(int institutionId, int applicationId, [Bind(Prefix = "LogoutItem")]LogoutItemDetail logoutItemDetail)
表单如下所示:
<% using (Html.BeginForm<LogoutItemsController>( c => c.Create(0,0,null))) { %>
<%= Html.AntiForgeryToken() %>
<tr>
<td><%= Html.SubmitButton("btnAdd", "Add") %></td>
<td><%= Html.TextBox("LogoutItem.Path")%></td>
</tr>
<% } %>
编辑 表单操作 URL
/Applications/LogoutItems/Create/0/0
我已使用 MVC 路由调试器和 VS 调试器验证此 url 与下面的路由匹配。
路由
routes.MapRoute(null, "Applications/{controller}/{action}/{institutionId}/{applicationId}/")
当前 URL
Applications/Show?institutionId=1001&applicationId=3003
当我单步执行时,Create 方法,
- institutionId 的值为 1001,
- applicationId 的值为 >0,并且
- logoutItemDetail 具有表单提交的值
对于为什么 applicationId 参数似乎未初始化有什么想法吗?
编辑
做了更多测试,我发现如果我没有在表单中明确命名控制器/操作,
<% using (Html.BeginForm()) { %>
页面中的每个表单都会被发布,并且所有参数值都是正确的。在这种情况下,表单操作值是空字符串。如果页面上的每个表单都没有同时发布,这将是一个可接受的解决方案。
I am having trouble with an ASP.net MVC form Posting to an Action containing multiple parameters. The problem is only some of the parameters are being initialized.
The page has multiple forms each backed by a separate controller.
The controller action receiving the Post action looks like this
public ActionResult Create(int institutionId, int applicationId, [Bind(Prefix = "LogoutItem")]LogoutItemDetail logoutItemDetail)
The form looks like this:
<% using (Html.BeginForm<LogoutItemsController>( c => c.Create(0,0,null))) { %>
<%= Html.AntiForgeryToken() %>
<tr>
<td><%= Html.SubmitButton("btnAdd", "Add") %></td>
<td><%= Html.TextBox("LogoutItem.Path")%></td>
</tr>
<% } %>
EDIT
Form action URL
/Applications/LogoutItems/Create/0/0
I have verified this url matches the route below with the MVC Route Debugger, and with VS Debugger.
And the route
routes.MapRoute(null, "Applications/{controller}/{action}/{institutionId}/{applicationId}/")
The current URL
Applications/Show?institutionId=1001&applicationId=3003
When I step through, the Create method,
- institutionId has a value of 1001,
- applicationId has a value of 0, and
- logoutItemDetail has the values of the form submission
Any ideas as to why the applicationId parameter does not appear to be initialized?
Edit
Doing a little more testing, I found that if i do not explicitly name the controller/action in the form
<% using (Html.BeginForm()) { %>
Every form in the page is Posted, and all of the correct parameter values. In this case, the forms action value is an empty string. This would be an acceptable solution if every form on the page was not also posted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据您提供的信息,一种可能是您在
global.asax
文件中的默认路由之后添加了自定义路由。当您定义新路线时,您需要首先添加最具体的路线,然后逐步发展到更一般的路线,类似于筛子。如果是这种情况,只需将自定义路由移至代码中的更高位置,使其列在默认路由之前即可。 LogoutItemDetail 实例绑定成功的原因是您显式声明了绑定。更多代码将有助于做出更准确的评估,但根据您分享的内容,这是我最好的猜测......
From the information you provided, one possibility is that you added your custom route after the default route in the
global.asax
file. When you define new routes, you need to add the most specific routes first and work down to the more general, similar to a sieve. If that's the case, simply move your custom route higher in the code so that it is listed before the default route. The reason that your LogoutItemDetail instance bound successfully was because you explicitly declared the binding.More code would help to make a more accurate assessment, but based on what you shared, that's my best guess...
如果您导航到 /Applications/Show/1001/3003 会发生什么
???
如果将 applicadionId 的名称更改为其他名称会发生什么?也许这是一个保留字?
what happens if you navigate to /Applications/Show/1001/3003
???
what happens if you change the name of applicadionId to something else? maybe it's a reserved word?
我同意 Neil T 的观点。可能首先会选择另一条路线。如果您可以告诉我们所有其他映射的路线,那将会有所帮助(或者只是注释掉所有其他要测试的路线)。
事实上,我认为 JDPeckham 的调试是正确的,因为您描述的路由映射没有默认 {action} 参数,所以我想知道您的测试 url 是如何映射到 Create 方法的。因此,请尝试导航到 /Applications/Show/Create/1001/3003。如果该网址有效,那么我肯定会说这是路由条目。
I agree with Neil T. there's probably another route being picked first. If you could tell us all other mapped routes, that would help (or just comment out all the other routes to test).
In fact, I think JDPeckham's debugging is on the right track since the route mapping you describe doesn't default the {action} parameter, so I wonder how your test url is even getting mapped to the Create method. So try navigating to /Applications/Show/Create/1001/3003. If that url works, then I would definitely say it's the route entries.
这个答案仅供参考:)
像这样:[链接]
this answer only for particial view :)
like this : [a link]