Posting string value with "+" to action replaces value with space

90 views Asked by At

I've done a HTTP POST to an action with the form value:

name=123+456

But in my action it comes through as:

123 456

So the model binder replaces "+" with a space.

[HttpPost]
public JsonResult MyAction(string name)
{

Any ideas? Do i have to create a custom model binder or something?

EDIT:

This is posted in jquery as follows:

$.ajax({
...
data: "name=" + $('#signin-username').val()
...
});

Should i use JSON.stringify or something similar? Or do i have to manually encode?

2

There are 2 answers

2
p.s.w.g On BEST ANSWER

The + character is how spaces are usually encoded in URL's. Try encoding the +:

name=123%2B456

In your jQuery, use the encodeURIComponent method:

$.ajax({
    ...
    data: "name=" + encodeURIComponent($('#signin-username').val())
    ...
});
1
Nadrendion On

This is because when data is URL-encoded, the + means that there was a space before.

Try setting a different header declaring that it should be parsed as plain text instead.

Sorry for not telling you the headers but answering from an iPhone so have quite limited resources at the moment.