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

MrCode
  • 63,975
  • 10
  • 90
  • 112
Daniel Gustafsson
  • 1,725
  • 7
  • 37
  • 78
  • If you do `data: $("form").serialize(),` it should be available as any other form data. – adeneo Jul 18 '13 at 06:57
  • See http://stackoverflow.com/questions/6992585/jquery-deserialize-form – MiRaIT Jul 18 '13 at 07: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 – Daniel Gustafsson Jul 18 '13 at 07:03

2 Answers2

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.

MrCode
  • 63,975
  • 10
  • 90
  • 112
  • See my edit, you need to make a small change to the ajax request. – MrCode Jul 18 '13 at 07:25
  • 1
    When changing the data from: data: { "testData": $("form").serialize() }, to: data: { $("form").serialize() }, i got the error: (: expected) – Daniel Gustafsson Jul 18 '13 at 07:28
  • Yeah that worked :) So now i got the key and .net knows the values of the key automaticaly?:) – Daniel Gustafsson Jul 18 '13 at 07:31
  • 1
    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']` – MrCode Jul 18 '13 at 07:33
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);
}
karaxuna
  • 26,752
  • 13
  • 82
  • 117