Question T13863
Visible to All Users

Get all survey results in one field

created a year ago (modified a year ago)

Hi
I'm using angular and asp.net as a back-end. I would like to save the full survey result in one field in a data base. How could I achieve this.
in my survey.component.ts, I have this code snippet:

TypeScript
ngOnInit() { const survey = new Model(json); const baseApiUrl : string = environment.baseApiUrl ; survey.onComplete.add(function (sender, options) { // Display the "Saving..." message (pass a string value to display a custom message) options.showSaveInProgress(); const xhr = new XMLHttpRequest(); xhr.open("POST", baseApiUrl + '/api/Survey'); xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8"); xhr.onload = xhr.onerror = function () { if (xhr.status == 200) { // Display the "Success" message (pass a string value to display a custom message) options.showSaveSuccess(); // Alternatively, you can clear all messages: // options.clearSaveMessages(); } else { // Display the "Error" message (pass a string value to display a custom message) options.showSaveError("Erreur d'enregistrement"); } }; xhr.send(JSON.stringify(sender.data)); }); this.model = survey; //pour afficher le mode dans la page web }

In my API, C# controller I have this

C#
[Route("api/[controller]")] [ApiController] public class SurveyController : Controller { [HttpPost] public async Task<IActionResult> InsertClient([FromBody] SurveyJSData surveyJSData) { var client = new Client { NomComplet = surveyJSData.NomComplet, PrenomComplet = surveyJSData.PrenomComplet, DateNaissance = surveyJSData.DateNaissance }; Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<Client> entityEntry = _context.Client.Add(client); await _context.SaveChangesAsync(); return Ok(client);

I would like also to have an additional field like this

C#
allResult = surveyJSData.AllResult

but I'm not able to do that. I have tested various things
I have tables, page and other types of questions in my survey.
Thanks

Answers approved by surveyjs Support

created a year ago

Hello,

Thank you for the interest in our products. Yes, you are using the correct approach: save survey result as a serialized to string JSON object.
Could you specify - what exactly troubles are you experiencing? Is survey result JSON uploaded to your server?

Your code has some inconsistencies:
You are sending just a string with stringified JSON:
xhr.send(JSON.stringify(sender.data));
to the /api/Survey endpoint.
And at the same time shows us a function with another name (it will be another endpoint) and this function expects to receive not a string but an object:

Code
\[HttpPost\] public async Task<IActionResult> InsertClient(\[FromBody\] SurveyJSData surveyJSData)

This definitely will not work as you expecting.

This question is rather related to ASP.NET (Core) MVC functionality and is better to be asked on the corresponding resources - MS forums or stackoverflow.com and similar.

Thanks, Serge
SurveyJS Team

    Show previous comments (3)
    AT AT
    Andrew Telnov a year ago

      Hello,
      You can look how we implement it for ASP.Net. You can find examples on Form Management Examples page. There are two examples: ASP.Net Core and Domain Model that is written on ASP.Net as well.
      I want to add that your task is not related to SurveyJS Libraries. You need to pass a JSON object from a client to your .Net application.
      You may get a faster and better answer on .Net related forums.

      Thank you,
      Andrew
      SurveyJS Team

        Hi Andrew
        Thank you.
        I was able to solve the problem, actually my controller sees an JSON because the HttpPost sends the JSON.stringify(sender.data) in a JSON format.
        Using JsonDocument data type with Ser/Deser, I'm was to, not only get the full survey result in a string and save it in my database but access to a specific field of my survey to save it in a separate field of my database. Here is the code
        My data models

        C#
        public class Client //the data model { [Key] public int ClientId { get; set; } [Column(TypeName = "nvarchar(MAX)")] [MaxLength(int.MaxValue)] public string? AllData { get; set; } //one field to store the whole survey result from JSON.stringify(sender.data) [Column(TypeName = "varchar(50)")] public string? NomComplet { get; set; } = "char"; [Column(TypeName = "varchar(100)")] public string? PrenomComplet { get; set; } = "char"; }

        My controller

        C#
        [Route("api/[controller]")] [ApiController] public class SurveyController : Controller { [HttpPost] public async Task<IActionResult> InsertClient([FromBody] JsonDocument surveyJSData) { var surveyDataString = JsonSerializer.Serialize(surveyJSData); SurveyData? surveyDataJson = JsonSerializer.Deserialize<Client>(surveyJSData); var client = new Client { AllData = surveyDataString, NomComplet = surveyDataJson?.NomComplet, PrenomComplet = surveyDataJson?.PrenomComplet, }; Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<Client> entityEntry = _context.Client.Add(client); await _context.SaveChangesAsync(); return Ok(client);

          Glad to know your problem has been solved. Thank you for sharing.