我使用了一个接受routeValues的Begin.Form重载
<%
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呈现为:
<form method="post" action="/Home/Category?TestRoute1=test">
我需要更改htmlAttributes,这就是我使用的原因:
<%
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" />
<% }%>
结果是完全错误的:
<form method="post" id="frmTyreBySizeCar" action="/de/TyreSize.mvc/List?Count=12&Keys=System.Collections.Generic.Dictionary%....
我可以在Formhelper的源代码中看到原因。
有两种重载适用于我给定的参数:
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,那么就不会有以object作为参数的重载,一切都会按预期进行。
我需要一个可以接受RouteValues和htmlAttributes字典的变通方法。我看到有一些重载有一个额外的routeName,但这不是我想要的。
编辑:尤金展示了BeginForm的正确用法。
Html.BeginForm("Category", "Home",
new RouteValueDictionary { {"TestRoute1", "test"} },
FormMethod.Post,
new Dictionary<string, object> { {"id", "frmCategory"} }
)
https://stackoverflow.com/questions/1038386
复制相似问题