简体   繁体   English

缺少参数初始化的Asp.net MVC表单发布至操作

[英]Asp.net MVC Form Post to Action with missing parameter initialization

I am having trouble with an ASP.net MVC form Posting to an Action containing multiple parameters. 我在使用ASP.net MVC表单发布到包含多个参数的Action时遇到麻烦。 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 接收到Post动作的controller动作如下所示

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. 我已经验证了该网址与MVC Route Debugger和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, 当我逐步执行Create方法时,

  • institutionId has a value of 1001 , stitutionalId的值为1001
  • applicationId has a value of 0 , and applicationId的值为0 ,并且
  • logoutItemDetail has the values of the form submission logoutItemDetail具有表单提交的值

Any ideas as to why the applicationId parameter does not appear to be initialized? 关于为什么applicationId参数似乎未初始化的任何想法?

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. 如果页面上的每个表单都没有发布,这将是一个可以接受的解决方案。

From the information you provided, one possibility is that you added your custom route after the default route in the global.asax file. 根据您提供的信息,一种可能性是您在global.asax文件中的默认路由之后添加了自定义路由。 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. LogoutItemDetail实例成功绑定的原因是因为您明确声明了绑定。

More code would help to make a more accurate assessment, but based on what you shared, that's my best guess... 更多代码有助于做出更准确的评估,但是根据您共享的内容,这是我最好的猜测...

what happens if you navigate to /Applications/Show/1001/3003 导航到/ Applications / Show / 1001/3003会发生什么

??? ???

what happens if you change the name of applicadionId to something else? 如果将applicadionId的名称更改为其他名称,会发生什么? maybe it's a reserved word? 也许这是保留字?

I agree with Neil T. there's probably another route being picked first. 我同意尼尔·T(Neil T)的观点。可能首先会选择另一条路线。 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. 实际上,我认为JDPeckham的调试是在正确的轨道上,因为您描述的路由映射没有默认使用{action}参数,因此我想知道您的测试U​​RL如何甚至映射到Create方法。 So try navigating to /Applications/Show/ Create /1001/3003. 因此,尝试导航到/ Applications / Show / 创建 / 1001/3003。 If that url works, then I would definitely say it's the route entries. 如果该网址有效,那么我肯定会说这是路线条目。

I've had something like that , 
found the solution as ; 

you can write static 

## <form action="/Video/VideoComment" ....  ## but 
you dont use For<> ; for example ; 

in using blog to use ;
## @using (Html.BeginForm(  ##
## @Html.DropDownListFor  ##


in statict ##  < form   ##  to use ; 
## @Html.DropDownList  ## 

this answer only for particial view :) like this : [a link] 这个答案仅适用于局部视图:)像这样:[一个链接]

nice development :)) 

sory my english :(( 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM