Base solution for your next web application
Open Closed

Issue - Saving data to appService #12185


User avatar
0
Bernard created

Hi,

I encounter a issue when I want to save my data the following error is displayed :

IThe JSON value could not be converted to System.Collections.Generic.List

Here is my payload:

{ "id": "2", "Questions": { "0.Text": "DDD", "0.Type": "Radio", "0.Answers": { "0.Text": "DDD", "1.Text": "DDDD" } }, "intitule": "DEMO", "descriptif": "", "dateDebut": "09/10/2024 09:54:05", "dateFin": "09/10/2024 09:54:05" }

The js code : ` var questionnaire = _$questionnaireInformationForm.serializeFormToObject();

abp.ui.setBusy();
_questionnairesService
    .createOrEdit(questionnaire)
    .done(function () {
        abp.notify.info(app.localize('SavedSuccessfully'));

        abp.event.trigger('app.createOrEditQuestionnaireModalSaved');
    })
    .always(function () {
        abp.ui.setBusy();
    });

};`

app service public virtual async Task CreateOrEdit(CreateOrEditQuestionnaireDto input) { if (input.Id == null) { await Create(input); } else { await Update(input); } }

The model CreateOrEditQuestionnaireDto :

` public class CreateOrEditQuestionnaireDto : EntityDto<int?> {

  [Required]
  [StringLength(QuestionnaireConsts.MaxIntituleLength, MinimumLength = QuestionnaireConsts.MinIntituleLength)]
  public string Intitule { get; set; }

  [StringLength(QuestionnaireConsts.MaxDescriptifLength, MinimumLength = QuestionnaireConsts.MinDescriptifLength)]
  public string Descriptif { get; set; }

  public DateTime DateDebut { get; set; }

  public DateTime DateFin { get; set; }

  public QuestionDto Question { get; set; }
  public List<QuestionDto> questions { get; set; }

}`

List question class :

` public class QuestionDto : EntityDto { public string Text { get; set; }

 public bool Obligatoire { get; set; }

 public string Type { get; set; }

 public string Image { get; set; }

 [NotMapped]
 public int Index { get; set; }

 public virtual int Number { get; set; }

 public int Score { get; set; }

 public int SurveyId { get; set; }

 public List<AnswerResponseDto> Answers { get; set; }

}`

Thks for help Bernard


2 Answer(s)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi Bernard

    Your payload contains nested objects like:

    {
      "Questions": {
        "0.Text": "DDD",
        "0.Type": "Radio",
        "0.Answers": {
          "0.Text": "DDD",
          "1.Text": "DDDD"
        }
      }
    }
    

    But this isn't how ASP.NET Core expects lists (arrays) to be sent. It expects a structure like:

    {
      "Questions": [
        {
          "Text": "DDD",
          "Type": "Radio",
          "Answers": [
            { "Text": "DDD" },
            { "Text": "DDDD" }
          ]
        }
      ]
    }
    

    In your case, Questions should be a list of QuestionDto, and Answers should be a list of AnswerDto.

  • User Avatar
    0
    Bernard created

    HI,

    I had to convert my Json like following

    `var questionnaire = _$questionnaireInformationForm.serializeFormToObject();

    function convertJsonToArray(oldJson) {

    var newJson = {
        "Questions": [],
        "intitule": oldJson.intitule,
        "descriptif": oldJson.descriptif,
        "dateDebut": oldJson.dateDebut,
        "dateFin": oldJson.dateFin
    };
    
    
    $.each(oldJson.Questions, function (index, questionData) {
        if (index.includes("Text")) {
            // Extraire la question
            var questionIndex = index.split(".")[0];
            var question = {
                "Text": questionData,
                "Type": oldJson.Questions[questionIndex + ".Type"],
                "Answers": []
            };
    
       
            $.each(oldJson.Questions[questionIndex + ".Answers"], function (answerKey, answerValue) {
                if (answerKey.includes("Text")) {
                    question.Answers.push({ "Text": answerValue });
                }
            });
    
      
            newJson.Questions.push(question);
        }
    });
    
    return newJson;
    

    }

    // Conversion var newJson = convertJsonToArray(questionnaire); debugger

    console.log(JSON.stringify(newJson, null, 4));

    abp.ui.setBusy(); _questionnairesService .createOrEdit(newJson)`......