2

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

6

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())
    ...
});
Sign up to request clarification or add additional context in comments.

2 Comments

doens't do anything... tried in chrome developer console. encodeURI('123+456') returns 123+456
@RPM1984 Sorry, I should have double-checked that. Try encodeURIComponent.
1

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.

1 Comment

The JSON.stringify doesn't help you with the encoding, unfortunately. See if jquery has some URL-encoding function you can use before sending the data. See stackoverflow.com/questions/6544564/…

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.