问题描述
我使用接受routeValues Begin.Form过载
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" />
<% }%>
这工作不错,呈现formtag为:
This works nice and renders the formtag as:
<form method="post" action="/Home/Category?TestRoute1=test">
我需要改变htmlAttributes,这就是为什么我用:
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.
有适用于我的给定参数2重载:
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)
据出错,因为第一种方法回升。如果我不供应htmlAttributes,再有就是用对象作为参数和everyrthing按预期工作。无过载
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.
我需要接受RouteValues和htmlAttributes的字典一个解决方法。我看到有一些有额外的routeName重载,但那不是我想要的。
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.
编辑:尤金显示BeginForm的使用权
eugene showed the right usage of BeginForm.
Html.BeginForm("Category", "Home",
new RouteValueDictionary { {"TestRoute1", "test"} },
FormMethod.Post,
new Dictionary<string, object> { {"id", "frmCategory"} }
)
推荐答案
使用(包括RouteValues和HtmlAttributes是对象):
Use (both RouteValues and HtmlAttributes are objects):
Html.BeginForm("Category", "Home",
new { TestRoute1 = "test" },
FormMethod.Post,
new { id = "frmCategory" }
)
或(两者RouteValues和HtmlAttributes是字典):
or (both RouteValues and HtmlAttributes are dictionaries):
Html.BeginForm("Category", "Home",
new RouteValueDictionary { {"TestRoute1", "test"} },
FormMethod.Post,
new Dictionary<string, object> { {"id", "frmCategory"} }
)
这篇关于Begin.Form与接受routeValues和htmlAttributes超载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!