7

I am not new to MVC so I am a bit baffled at why I can't change the URL of my POST when I click the submit button.

I have this simple view called 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"/>
}

Because of it's simplicity I am not using a model, I only want to POST the txt value. However the URL is a bit odd. It's pointing to this path in the site:

<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>

I don't understand where it's getting length=18 from. I want to post to this method:

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

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

Some help please?

Gilsha
  • 14,431
  • 3
  • 32
  • 47
nick gowdy
  • 6,191
  • 25
  • 88
  • 157

3 Answers3

7

Replace @using (Html.BeginForm("PandoraGetDocsList")) to @using (Html.BeginForm())

PandoraGetDocsList is the string of length 18 which you are getting in your post

If you want to redirect it to the action PandoraGetDocsList then do like this:

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

Explanation: Html.BeginForm does not accept a parameter as string.

Arijit Mukherjee
  • 3,817
  • 2
  • 31
  • 51
3

There is no overload that accepts just a string as parameter. It's using the BeginForm(this HtmlHelper htmlHelper, Object routeValues) overload and because of that, it attempts to serialize your string which is passed as an object.

In the case of a string object, the only public property is Length, and since there will be no routes defined with a Length parameter it appends the property name and value as a query string parameter.

The overload you're looking for is BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName).

@using (Html.BeginForm("PandoraGetDocsList", "controller name here"))
Community
  • 1
  • 1
user247702
  • 23,641
  • 15
  • 110
  • 157
-1

Use the overload. @using (Html.BeginForm("PandoraGetDocsList", null))

Cas Bloem
  • 4,846
  • 2
  • 24
  • 23