【问题标题】:Why does BeginForm("Foo") add a "Length" key to the query string?为什么 BeginForm("Foo") 会在查询字符串中添加“Length”键?
【发布时间】:2016-01-19 22:09:36
【问题描述】:

我对 MVC 并不陌生,所以我有点困惑为什么我在单击提交按钮时无法更改 POST 的 URL。

我有一个名为 PandoraRemovalTool.cshtml 的简单视图

@{
    ViewBag.Title = "PandoraRemovalTool";
}
@using (Html.BeginForm("PandoraGetDocsList"))
{
    <h2>Pandora Removal Tool</h2>    
    @Html.Label("Member number:")
    @Html.TextBox("txtMemberNumber")
    <br />
    <input type="submit" value="Search"/>
}

因为它很简单,我没有使用模型,我只想发布 txt 值。但是 URL 有点奇怪。它指向站点中的这条路径:

<form action="/Administration/PandoraRemovalTool?Length=18" method="post" novalidate="novalidate">    
    <h2>Pandora Removal Tool</h2>
    <label for="Member_number:">Member number:</label>
    <input id="txtMemberNumber" name="txtMemberNumber" type="text" value=""/>        
    <br>
    <input type="submit" value="Search"/>
</form>

我不明白它是从哪里得到 length=18 的。我想发布到这个方法:

[HttpPost]
public ActionResult PandoraGetDocsList(string txtMemberNumber)
{
    return RedirectToAction("PandoraRemovalTotal2", new {MemberNum = txtMemberNumber });
}

public ActionResult PandoraRemovalTotal2(string MemberNum)
{
    return View();
}

请帮忙?

【问题讨论】:

  • PandoraGetDocsList 是你得到的长度 18

标签: asp.net-mvc-4 post html.beginform


【解决方案1】:

@using (Html.BeginForm("PandoraGetDocsList")) 替换为@using (Html.BeginForm())

PandoraGetDocsList 是您在帖子中收到的长度为 18 的字符串

如果您想将其重定向到操作PandoraGetDocsList,请执行以下操作:

@using (Html.BeginForm("PandoraGetDocsList", "Administration", new { txtMemberNumber = someString }))

说明: Html.BeginForm 不接受字符串形式的参数。

【讨论】:

  • 感谢工作。在我发布到 SO 之前,我做了一些非常相似的事情,但它没有用,所以我一定错过了一些愚蠢的事情。
  • 否决投票者有任何 cmets/原因?
【解决方案2】:

没有只接受string 作为参数的重载。它使用BeginForm(this HtmlHelper htmlHelper, Object routeValues) 重载,因此,it attempts to serialize 你的string 作为object 传递。

string 对象的情况下,唯一的公共属性是Length,由于没有使用长度参数定义的路由,它会将属性名称和值作为查询字符串参数附加。

您要查找的重载是BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName)

@using (Html.BeginForm("PandoraGetDocsList", "controller name here"))

【讨论】:

    【解决方案3】:

    使用重载。 @using (Html.BeginForm("PandoraGetDocsList", null))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      • 2022-08-16
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      相关资源
      最近更新 更多