asp.net-mvc Begin.Form 带有接受 routeValues 和 htmlAttributes 的重载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1038386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Begin.Form with overload that accepts routeValues and htmlAttributes
提问by Mathias F
I use an overload of Begin.Form that accepts routeValues
我使用接受 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" />
<% }%>
This works nice and renders the formtag as:
这很好用,并将 formtag 呈现为:
<form method="post" action="/Home/Category?TestRoute1=test">
I need to change htmlAttributes, thats why I used:
我需要更改 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" />
<% }%>
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.
我可以在 Formhelper 的来源中看到原因是什么。
There are 2 overloads that apply to my given parameters:
有 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)
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.
出错了,因为第一种方法被捡到了。如果我不提供 htmlAttributes,则对象作为参数不会过载,并且一切都按预期工作。
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.
我需要一个接受 RouteValues 和 htmlAttributes 字典的解决方法。我看到有些重载有一个额外的 routeName,但这不是我想要的。
EDIT: eugene showed the right usage of BeginForm.
编辑:eugene 展示了 BeginForm 的正确用法。
Html.BeginForm("Category", "Home",
new RouteValueDictionary { {"TestRoute1", "test"} },
FormMethod.Post,
new Dictionary<string, object> { {"id", "frmCategory"} }
)
)
回答by eu-ge-ne
Use (both RouteValues and HtmlAttributes are objects):
使用(RouteValues 和 HtmlAttributes 都是对象):
Html.BeginForm("Category", "Home",
new { TestRoute1 = "test" },
FormMethod.Post,
new { id = "frmCategory" }
)
or (both RouteValues and HtmlAttributes are dictionaries):
或(RouteValues 和 HtmlAttributes 都是字典):
Html.BeginForm("Category", "Home",
new RouteValueDictionary { {"TestRoute1", "test"} },
FormMethod.Post,
new Dictionary<string, object> { {"id", "frmCategory"} }
)
回答by David
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>
回答by Reza
you can write
你可以写
<% using (Html.BeginForm("Category", "Home", new {TestRoute1=HttpContext.Current.Request.QueryString["TestRoute1"] }, FormMethod.Post, new {id = "MainForm" })) { %>

