MVC3 Html.BeginForm - passing arguments as RouteValueDictionary fails

…衆ロ難τιáo~ 提交于 2019-11-30 20:06:55

args was generated by a filter, and is a RouteValueDictionary

That's the key point here. In this case make sure you are using the correct overload of the BeginForm method:

@using(Html.BeginForm(
    "Profile", 
    "Account",   
    args, 
    FormMethod.Post, 
    new RouteValueDictionary(new { @class = "mainForm" })
))
{
    ...
}

Notice the last argument? It must be an IDictionary<string, object> for this to work.

In your example it is this overload that gets picked up. But since you are passing a RouteValueDictionary for the routeValues parameter instead of an anonymous object it gets messed up.

So, you should either have both routeValues and htmlAttributes as dictionaries or both as anonymous objects.

Usama Khalil

Following will work.

 @using (Html.BeginForm("Profile", "Account", new { id=122}, FormMethod.Post, new { @class = "mainForm" }))

the route value is created by object initialize syntax i.e new {key = value}

So you're using this overload:

http://msdn.microsoft.com/en-us/library/dd460542.aspx

Is it possible to make args a simple object with keys and values? I think that might solve your problem.

According to docs:

The parameters are retrieved through reflection by examining the properties of the object. This object is typically created by using object initializer syntax. - this seems to be what is happening-- it's using reflections to get the properties of route dictionary- the properties being keys (collection of string) and values (collection of objects)

Another option would be to not use the html helper and create the form tag manually-- although that would kind of defeat the purpose of having the html helpers.

Just a thought, but, even for a multi-step form, wouldn't you want to either choose to make it all GET or POST? In the example above... it looks like you are using POST with the form... but still trying to use GET along the way.

Why not just use hidden POST values (using HTML INPUTs,) along the way?

Otherwise, users could more-easily change the values, right? (Though, that might not matter in this application. And this is mostly just food for thought.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!