tags:

views:

736

answers:

2

I use an overload of Begin.Form that accepts routeValues

    <% 
        RouteValueDictionary routeValues = ViewContext.RouteData.Values;
        routeValues.Add("TestRoute1", "test");

        using (Html.BeginForm(
            "Category", 
            "Home",
              routeValues,
              FormMethod.Post

      ))
       { %>  

        <input type="submit" value="submit" name="subform" />
<% }%>

This works nice and renders the formtag as:

<form method="post" action="/Home/Category?TestRoute1=test">

I need to change htmlAttributes, thats why I used:

    <% 
        RouteValueDictionary routeValues = ViewContext.RouteData.Values;
        routeValues.Add("TestRoute1", "test");

        using (Html.BeginForm(
            "Category", 
            "Home",
              routeValues,
              FormMethod.Post,
              new {id="frmCategory"}

      ))
       { %>  

        <input type="submit" value="submit" name="subform" />
<% }%>

The result is completely wrong:

<form method="post" id="frmTyreBySizeCar" action="/de/TyreSize.mvc/List?Count=12&amp;Keys=System.Collections.Generic.Dictionary%....

I can see in the source of the Formhelper what the reason is.

There are 2 overloads that apply to my given parameters:

public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method, object htmlAttributes)

public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, FormMethod method, IDictionary<string, object> htmlAttributes)

It goes wrong, because the first method is picked up. If I dont supply htmlAttributes, then there is no overload with object as a parameter and everyrthing works as expected.

I need a workaround that accepts a Dictionary of RouteValues and htmlAttributes. I see that there are overloads that have an additional routeName, but thats not what I want.

EDIT: eugene showed the right usage of BeginForm.

Html.BeginForm("Category", "Home",
new RouteValueDictionary { {"TestRoute1", "test"} },
FormMethod.Post,
new Dictionary<string, object> { {"id", "frmCategory"} }

)

A: 
using (Html.BeginForm("Category", "Home", new { TestRoute1="test"}, 
       FormMethod.Post, new {id="frmCategory"})) {

renders to

<form action="/accountchecker/Home/Category?TestRoute1=test" 
    id="frmCategory" method="post">
    <input type="submit" value="submit" name="subform" />
</form>
David Liddle
+4  A: 

Use (both RouteValues and HtmlAttributes are objects):

Html.BeginForm("Category", "Home",
    new { TestRoute1 = "test" },
    FormMethod.Post,
    new { id = "frmCategory" }
)

or (both RouteValues and HtmlAttributes are dictionaries):

Html.BeginForm("Category", "Home",
    new RouteValueDictionary { {"TestRoute1", "test"} },
    FormMethod.Post,
    new Dictionary<string, object> { {"id", "frmCategory"} }
)
eu-ge-ne
Thank you eugene,it works.But needs to be new Dictionary<string, object> { {"id", "frmCategory"}
Malcolm Frexner
Thanks, fixed :)
eu-ge-ne