开发者

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

开发者 https://www.devze.com 2022-12-22 00:19 出处:网络
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.

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.


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...


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?


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.


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 :(( 
0

精彩评论

暂无评论...
验证码 换一张
取 消