【问题标题】:Use MVC Html.BeginForm with GET method and action parameters将 MVC Html.BeginForm 与 GET 方法和操作参数一起使用
【发布时间】:2012-08-13 20:17:12
【问题描述】:

我知道我可以使用 Html.BeginForm 使用如下操作参数进行 POST:

@using (Html.BeginForm("Details", "Home", new { name = Model.Name }))

我可以像这样得到:

@using (Html.BeginForm("Details", "Home", FormMethod.Get))

但我似乎不能两者兼得。我该怎么做?

更新

以下是相关表格的全部内容:

@using (Html.BeginForm("Details", "Home", new { name = Model.Name }))
{
    <div style="text-align: center">
        @Html.DropDownList("viewMonth", 
                                  new List<SelectListItem>(ViewBag.MonthNames))
        @Html.TextBox("viewYear", Model.ViewYear, new {style = "width: 30px"})
        <input type="submit" value="Go"/>
        <br/>
        <br/>
    </div>
}

其中viewMonthviewYear 是操作/详细信息中的参数名称。如果我尝试

@using (Html.BeginForm("Details", "Home", new { name = Model.Name }, 
                        FormMethod.Get))

它将传递 viewMonth 和 viewYear,但不传递名称。

【问题讨论】:

  • 您想以一种形式进行 GET 和 POST 吗?
  • 不,请原谅我出色的沟通。我正在尝试以一种形式获取和使用操作参数(我想是 RouteValues)。

标签: asp.net asp.net-mvc razor


【解决方案1】:

但我似乎不能两者兼得

你可以同时做...你可以使用你需要的overload

例如,如果namerouteValue

@using (Html.BeginForm("Details", "Home", new { name = Model.Name }, 
                        FormMethod.Get))

如果名称是htmlAttribute 的形式:

@using (Html.BeginForm("Details", "Home", FormMethod.Get, 
                        new { name = Model.Name }))

如果你想同时使用 routeValues 和 htmlAttributes:

@using (Html.BeginForm("Details", "Home", new { name = Model.Name }, 
                        FormMethod.Get, new { name = "foo_bar" }))

【讨论】:

  • 感谢您的回复。我想 name 是一个 routeValue,因为我打算将它作为一个 action 参数,但讽刺的是,第一个代码段会从 ViewData 中获取另外两个 routeValue,但省略 name。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-25
相关资源
最近更新 更多