0

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?

4
  • 1
    What's the generated HTML? Commented Jul 23, 2013 at 19:05
  • <form action="/Controller/MyAction/Invalid#name%x" method="post"> </form> Commented Jul 24, 2013 at 13:16
  • That looks like a bug in MVC. Which version? Commented Jul 24, 2013 at 13:40
  • Yes, it looks like a bug, because Html.BeginForm("ActionName", "Controller") works fine, but Html.BeginForm() doesn't. Commented Jul 24, 2013 at 14:26

2 Answers 2

2

Pass ActionName and Controller in your form

@using (Html.BeginForm("ActionName", "Controller")) {
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

My intention was to make the RESTful URLs like domain/controller/action/[human readable entity name] in order to make them looks informative for humans and avoid additional parameters.
@Omnituens I think then you should use _ or - in place of space.. as you can see in stack overflow Uri..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.