MVC3 Html.BeginForm - passing arguments as RouteValueDictionary fails

后端 未结 4 1405
梦毁少年i
梦毁少年i 2021-01-05 05:04

I have a multi-step setup process where I would like to pass query string arguments appended to the URL only if they are relevant.

http://localhost:6618/Account/Prof

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-05 05:10

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

提交回复
热议问题