Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Begin.Form with overload that accepts routeValues and htmlAttributes

Tags:

asp.net-mvc

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"} }

)

like image 446
Mathias F Avatar asked Jun 24 '09 13:06

Mathias F


People also ask

What is the use of@ using HTML BeginForm())?

BeginForm(HtmlHelper, String, String, FormMethod, Object)Writes an opening <form> tag to the response and sets the action tag to the specified controller and action. The form uses the specified HTTP method and includes the HTML attributes.

What is HTML BeginForm in MVC 5?

BeginForm is the Html Helper Extension Method that is used for creating and rendering the form in HTML. This method makes your job easier in creating form. Here, is the method to create a form using Html. BeginForm extension method in ASP.NET MVC5. BeginForm("ActionMethod", "ControllerName","Get⁄Post Method")

How do you pass a route value in HTML BeginForm?

The value will only be added as query string values if its FormMethod. Get (and you can remove the route values from the BeginForm() method). But what is the point of this - they are hidden inputs, not editable values.


2 Answers

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"} }
)
like image 124
eu-ge-ne Avatar answered Oct 20 '22 00:10

eu-ge-ne


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>
like image 41
David Avatar answered Oct 20 '22 00:10

David