【发布时间】:2011-05-26 20:20:00
【问题描述】:
我使用了接受 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" />
<% }%>
这很好用,并将表单标签呈现为:
<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的源码中看到是什么原因。
有 2 个重载适用于我的给定参数:
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,那么对象作为参数就不会出现重载,并且一切都按预期工作。
我需要一个接受 RouteValues 和 htmlAttributes 字典的解决方法。我看到有些重载有一个额外的 routeName,但这不是我想要的。
编辑:eugene 展示了 BeginForm 的正确用法。
Html.BeginForm("Category", "Home",
new RouteValueDictionary { {"TestRoute1", "test"} },
FormMethod.Post,
new Dictionary<string, object> { {"id", "frmCategory"} }
)
【问题讨论】:
标签: asp.net-mvc