Omnituens
Omnituens

Reputation: 15

ASP.NET MVC Html.BeginForm decodes URL

I'm requesting ASP.NET MVC the controller using the URL like this:

http://mysite.com/controller/myaction/Invalid%23name%25x

where Invalid%23name%25x is a parameter to

public ActionResult MyAction(string id) {
  return View();
}

The GET request works fine.

MyAction view looks like this:

@using (Html.BeginForm()) {
   ...
   <input name="Save" type="submit" value="Save" />
}

The generated HTML is:

<form action="/Controller/MyAction/Invalid#name%x" method="post">
    ...
    <input name="Save" type="submit" value="Save" />
</form>

When I click on "Save", the form gets posted and the POST request goes to

http://mysite.com/controller/myaction/Invalid#name%x

i.e. the initial URL is decoded. This means the the POST action receives only the first part of the parameter - "Invalid"

[HttpPost]
public ActionResult MyAction(string id, ...) {
  return View();
}

How can I prevent Html.BeginForm from decoding the initial URLs in order to preserve the initial state?

Upvotes: 0

Views: 6003

Answers (2)

Satpal
Satpal

Reputation: 133403

Pass ActionName and Controller in your form

@using (Html.BeginForm("ActionName", "Controller")) {

Upvotes: 2

techBeginner
techBeginner

Reputation: 3850

I would personally recommend you not to use id as string because as you have seen string can have many words in it.. let it mean what usually it does(numeric value).

use something like http://mysite.com/controller/myaction?Name=Invalid%23name%25x

public ActionResult MyAction(string Name) {
    return View();
}

I suppose this would work for you..

Upvotes: 0

Related Questions