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&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"} }
)
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.
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")
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.
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"} }
)
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With