2

I'm trying to send a json serialized object through a form to a c# MVC action.

var object = {
  id: 1,
  field1: "",
  field2: "",
  .
  .
  .
}

var inputs = "<input type'hidden' name='serializedObject' value='" + JSON.stringify(object) + "'/>";
$("<form action='actionUrl' method='POST' >" + inputs  + "</form>").appendTo("body").submit().remove();

Server side I have an action that take stringified object and parse them:

[HttpPost]
public virtual FileResult TestAction(string serializedObject){
    //...do stuff....
}

But in the action I don't receive the entire json string (I have to use form and not ajax because I have to download a file).

2
  • 1
    Which json part do you get? Commented Mar 8, 2016 at 18:40
  • It not really clear why you are doing a POST. If your wanting to download file based on some values then you can just build a url e.g.var url = '/../TestAction?id=1&field1=someValue&field2=anotherValue etc and use location.href=url; where the method is FileResult TestAction(int id, string field1, etc) Commented Mar 9, 2016 at 2:05

1 Answer 1

2

I dont know if that is your problem but If any of your data values contains single quotes, the next values after this single quotes will not be sent, single quotes are not valid.

It's recommendable that all fields and values must be surrounded by double quotes.

When I sent the object like this:

var object = {
            "id": "1",
            "field1": "Its",
            "field2": "working",
            "field3": "Fine!"
        }

I got this in action:

{"id":"1","field1":"Its","field2":"working","field3":"Fine!"}

But when I sent this:

var object = {
                "id": "1",
                "field1": "It's",
                "field2": "working",
                "field3": "Fine!"
            }

I got incomplete values:

{"id":"1","field1":"It

You can substitute the single quotes by &#39 ("field1": "It&#39s",) , that works too.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok the problems was that I use the double quotes to delimitate the attributes in the html input and when I insert serialized object in the value attribute this is broken at the first double quote.
@Noob 1º object has this property and value = "field1": "Its" the value from same property on the 2º object is: "field1": "It's" On the 2º object we have a single quote("it" + ' ' ' + "s"), that's the difference.

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.