1

I have a dynamic build form, buildt from a textfile so I don't know the names of the variables in advance. I have serialized the form and the values to a json string:

"PersonId=aasd&Gender=Kvinna&Education=sd&RelativeDementia=Ja&Apoe4=Vet+ej&
SystolicBT=asd&HypertoniaTreatment=Nej&FPColesterol=asd&PersonLength=asdas
&PersonWeight=dasd&PersonBMI=asd&AbdominalCircumference=adsasd
&KnownDiabetesMellitus=Ja&HadTIAStroke=Ja
&KnownHeartDisease=Ja
&IsCurrentlySmoking=Ja&IsExperiencingStress=Nej
&KnownDepression=Ja%2C+tidigare+behandlad&PhysicallyActive=Ja" 

with this method:

$(document).on("click", "#btnsubmit", function() {
    $.ajax({
        url: "/Home/RiskScore",
        type: "post",
        data: { "testData": $("form").serialize() },
        success: function(result) {
        }

and now I want to deserialize it so that I can show the values and the name for each value on the next page. I have tried a lot of different variations of code but have not succeded. Hope you can help!

Thanks

3
  • If you do data: $("form").serialize(), it should be available as any other form data. Commented Jul 18, 2013 at 6:57
  • See stackoverflow.com/questions/6992585/jquery-deserialize-form Commented Jul 18, 2013 at 7:02
  • If i do: $(document).on("click", "#btnsubmit", function() { $.ajax({ url: "/Home/RiskScore", type: "post", data: $("form").serialize(), success: function(result) { } it sends null to my homecontroller Commented Jul 18, 2013 at 7:03

2 Answers 2

1

You haven't got JSON data, it's standard URL encoded notation. If you want to access that on the server side you can loop over the post data. ASP.NET automatically parses this format into the Request.Form collection.

foreach(string key in Request.Form.AllKeys)
    Response.Write(Request.Form[key]);

You also need to change the AJAX to:

data: $("form").serialize(),

The reason for this is because you don't want the testData identifier, you just want the raw POST data as the data property.

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

4 Comments

See my edit, you need to make a small change to the ajax request.
When changing the data from: data: { "testData": $("form").serialize() }, to: data: { $("form").serialize() }, i got the error: (: expected)
Yeah that worked :) So now i got the key and .net knows the values of the key automaticaly?:)
Yes, all of the key and values are auto parsed into the Request.Form collection so you can either loop over them like in the answer, if you don't know the keys. Or if you know the keys you can directly access them Request.Form['PersonId']
0

You can use serializeArray method to get form values as name-value pairs:

var nameValues = $("form").serializeArray();
for(var i = 0; i < nameValues.length; i++){
    console.log(nameValues[i].name);
    console.log(nameValues[i].value);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.