1

I have tried to save data to json file using angular js $http post to asp.net controller

Here is my codes

view with angular js

 var postdata =
                             {

                                 Email: "[email protected]",
                                 selectedanswers: jsonArr
                             };
        $http({
            url: "/page/PostFileWithData",
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            },
            method: "POST",
            dataType: "json",
            traditional: true,
            data: JSON.stringify(postdata)
        }).success(function (data) {
            console.log(data);
        }).error(function (data) {
            console.log('fail');
        });

Modal is asp.net c#

public class UserModel
    {

        public string Email { get; set; }
        //public string selectedanswers { get; set; }
        //public Dictionary<int, int> selectedanswers { get; set; }

        //public List<selectedanswersModel> selectedanswers { get; set; }
        public List<Answer> selectedanswers { set; get; }

    }


    public class Answer
    {

        public int AnswerId { set; get; }
    }

Controller in asp.net

[HttpPost]
    public JsonResult PostFileWithData(UserModel userdata)
    {


        UserModel udata = new UserModel
        {
            Email = userdata.Email,
            selectedanswers = userdata.selectedanswers
        };

        var json = System.IO.File.ReadAllText(Server.MapPath("~/App_Data/data.json"));


          JArray array = JArray.Parse(json);
          var itemToAdd = new JObject();
          itemToAdd["Email"] = userdata.Email;
          itemToAdd["selectedanswers"] = userdata.selectedanswers;  
          array.Add(itemToAdd);

          var jsonToOutput = JsonConvert.SerializeObject(array, Formatting.Indented);

          System.IO.File.WriteAllText(Server.MapPath("~/App_Data/data.json"), jsonToOutput);




        return Json(udata, JsonRequestBehavior.AllowGet);


    }

My problem is in controller action JsonResult which at where i wanted to save posted data to data.json file this only storing Email data but not selected answers regarding this

itemToAdd["selectedanswers"] = userdata.selectedanswers; 

this line of code to store at the index, i am getting error in this line so selectedanswers not storing to json file, i am receiving this data at console.log

here is my console.log result

[console result][1]

Can you please help me? Thanks

9
  • what are you getting in userdata.selectedanswers on serverside Commented May 7, 2018 at 5:48
  • its giving me selected data like this 0 : {AnswerId: 3} 1 : {AnswerId: 2} 2 : {AnswerId: 0} 3 : {AnswerId: 3} 4 : {AnswerId: 2} 5 : {AnswerId: 3} 6 : {AnswerId: 1} 7 : {AnswerId: 3} 8 : {AnswerId: 2} 9 : {AnswerId: 0} 10 : {AnswerId: 3} 11 : {AnswerId: 2} 12 : {AnswerId: 0} Commented May 7, 2018 at 5:50
  • can you write like this itemToAdd["selectedanswers"] = userdata.selectedanswers.select(a=> a.AnswerId).ToArray(); Commented May 7, 2018 at 5:51
  • I have tried this, but showing another error at Select Commented May 7, 2018 at 6:02
  • 2
    do like this itemToAdd["selectedanswers"] =JArray.FromObject(userdata.selectedanswers.select(a=> a.AnswerId).ToArray()); Commented May 7, 2018 at 6:14

1 Answer 1

1

you can try like this

itemToAdd["selectedanswers"] 
     =JArray.FromObject(userdata.selectedanswers.select(a=> a.AnswerId).ToArray());

or try this

itemToAdd["selectedanswers"] 
     =JArray.FromObject(userdata.selectedanswers.select(a=> 
 new { AnswerId = a.AnswerId } ).ToArray());
Sign up to request clarification or add additional context in comments.

3 Comments

Supper @pranay Rana, it working like a charm, you are very helpful
hi @pranay rana can you please help me for this question? stackoverflow.com/questions/50213054/…
@MohammadSumon - i work on angular 2 and .net stuff not have much knowledge on angularjs , apologies

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.