为什么BeginForm(“Foo”)在查询字符串中添加“Length”键?

时间:2014-07-11 11:04:12

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

我不是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>

我不明白从哪里得到长度= 18。我想发布这个方法:

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

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

请帮忙吗?

3 个答案:

答案 0 :(得分:4)

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

PandoraGetDocsList是您在帖子中获得的长度为18的字符串

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

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

说明: Html.BeginForm不接受参数作为字符串。

答案 1 :(得分:2)

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

  

对于string对象,唯一的公共属性是Length,并且由于没有使用Length参数定义的路由,因此它将属性名称和值附加为查询字符串参数

您正在寻找的超负荷是BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName)

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

答案 2 :(得分:-1)

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