【问题标题】:Begin.Form with overload that accepts routeValues and htmlAttributesBegin.Form 具有接受 routeValues 和 htmlAttributes 的重载
【发布时间】: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&amp;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


    【解决方案1】:

    使用(RouteValues 和 HtmlAttributes 都是对象):

    Html.BeginForm("Category", "Home",
        new { TestRoute1 = "test" },
        FormMethod.Post,
        new { id = "frmCategory" }
    )
    

    或者(RouteValues 和 HtmlAttributes 都是字典):

    Html.BeginForm("Category", "Home",
        new RouteValueDictionary { {"TestRoute1", "test"} },
        FormMethod.Post,
        new Dictionary<string, object> { {"id", "frmCategory"} }
    )
    

    【讨论】:

    • 谢谢尤金,它有效。但需要 new Dictionary { {"id", "frmCategory"}
    • @eu-ge-ne 你能检查我的问题吗,有一些相似之处:stackoverflow.com/questions/12142356/…
    • 仅供参考,RouteValueDictionary 的字典构造函数仅在您使用字典 对象时才有效(以正确的方式)。我传入了 Dictionary,它可能使用了整个字典作为值,导致结果类似于上面的输出。
    【解决方案2】:
    using (Html.BeginForm("Category", "Home", new { TestRoute1="test"}, 
           FormMethod.Post, new {id="frmCategory"})) {
    

    呈现给

    <form action="/accountchecker/Home/Category?TestRoute1=test" 
        id="frmCategory" method="post">
        <input type="submit" value="submit" name="subform" />
    </form>
    

    【讨论】:

    • 对我来说它工作得很好,直到我添加了另一个参数。我的意思是使用 (Html.BeginForm("Category", "Home", new { TestRoute1="test", Category="cat1"}, FormMethod.Post, new {id="frmCategory"})) {...}第二个参数始终为空。真的很奇怪。
    【解决方案3】:

    你可以写

    <% using (Html.BeginForm("Category", "Home", new {TestRoute1=HttpContext.Current.Request.QueryString["TestRoute1"] }, FormMethod.Post, new {id = "MainForm" })) { %>
    

    【讨论】:

      猜你喜欢
      • 2013-08-06
      • 2016-12-05
      • 2023-03-03
      • 2020-06-03
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多