Base solution for your next web application

Activities of "Bernard"

Answer

Hi,

Thks it's was an abp.ui.clearBusy(); problem i set abp.ui.setBusy(); instead

Question

Hi,

I have issu when returning values in js on creating an new record . Here is my actually code :

From **QuestionnaireAppService **:

`[AbpAuthorize(AppPermissions.Pages_Questionnaires_Create)] protected virtual async Task Create(CreateOrEditQuestionnaireDto input) { var questionnaire = new Survey(); questionnaire.Intitule = input.Intitule; questionnaire.Descriptif = input.Descriptif; questionnaire.DateDebut = input.DateDebut; questionnaire.DateFin = input.DateFin;

if (AbpSession.TenantId != null)
{
    questionnaire.TenantId = (int?)AbpSession.TenantId;
}

await _questionnaireRepository.InsertAsync(questionnaire);
await CurrentUnitOfWork.SaveChangesAsync();

for (int i = 0; i < input.Questions.Count; i++)
{
    Question question = new Question
    {
        SurveyId = questionnaire.Id,
        Number = i + 1,
        Text = input.Questions[i].Text,
        Type = input.Questions[i].Type,
    };

    await _questionRepository.InsertAsync(question);
    await CurrentUnitOfWork.SaveChangesAsync();

    for (int j = 0; j < input.Questions[i].Answers.Count; j++)
    {
        Answer answer = new Answer
        {
            QuestionId = question.Id,
            Number = i + 1,
            Text = input.Questions[i].Answers[j].Text,
        };

        await _answerRepository.InsertAsync(answer);
        await CurrentUnitOfWork.SaveChangesAsync();
    }


}

}`

Js code :

` function save(successCallback) {

  debugger

  if (!_$questionnaireInformationForm.valid()) {
      return;
  }


  var oldJson = _$questionnaireInformationForm.serializeFormToObject();

  console.log(JSON.stringify(oldJson, null, 4));
  function convertJsonToArray(oldJson) {
      // Nouveau format avec des tableaux
      var newJson = {
          "Questions": [],
          "intitule": oldJson.intitule,
          "descriptif": oldJson.descriptif,
          "dateDebut": oldJson.dateDebut,
          "dateFin": oldJson.dateFin,
          "id": oldJson.id

      };

  
      $.each(oldJson.Questions, function (index, questionData) {
          if (index.includes("Text")) {
          
              var questionIndex = index.split(".")[0];
              var question = {
                  "Text": questionData,
                  "Type": oldJson.Questions[questionIndex + ".Type"],
                  "Answers": []
              };
              debugger
           
              $.each(oldJson.Questions[questionIndex + ".Answers"], function (answerKey, answerValue) {
                  if (answerKey.includes("Text")) {
                      question.Answers.push({ "Text": answerValue });
                  }
              });

           
              newJson.Questions.push(question);
          }
      });

      return newJson;
  }

  var newJson = convertJsonToArray(oldJson);


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

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

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

}; ` Create (appservice ) model :

` public class CreateOrEditQuestionnaireDto : EntityDto<int?> {

  public string Intitule { get; set; }

  public string Descriptif { get; set; }

  public DateTime DateDebut { get; set; }

  public DateTime DateFin { get; set; }

  public CreateOrEditQuestionnaireDto()
  {
      Questions = new List<QuestionDto>();
  }

  public List<QuestionDto> Questions { get; set; }

}`

View CreateOrEdit viewmodel :

` public class CreateOrEditQuestionnaireModalViewModel { public CreateOrEditQuestionnaireDto Questionnaire { get; set; }

 public bool IsEditMode => Questionnaire.Id.HasValue;

 public Survey Survey { get; set; }
 public QuestionDto Question { get; set; }

 public UserAccount User { get; set; }


 public List<QuestionDto> Questions { get; set; }


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

}` The recording is done well but the loader continues to run and the page is blocked.

Thks for help Bernard

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)`......

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

Answer

Hi Could you please share the link

Thks

Answer

Hi

Have you the time to make a sample ?

Answer

Hi oguzhanagir,

**I finally found the solution ! **

I must Map person entity with input this way in Creat method :

` [AbpAuthorize(AppPermissions.Pages_Persons_Create)] protected virtual async Task Create(CreateOrEditPersonDto input) { var person = ObjectMapper.Map

   if (AbpSession.TenantId != null)
   {
       person.TenantId = (int?)AbpSession.TenantId;
   }

   //For Test
   person.Id = 90;

   //await _personRepository.InsertAsync(person);
   //await CurrentUnitOfWork.SaveChangesAsync();

   ObjectMapper.Map(person, input);

}`

And in method CreateOrEdit send Output this way :

` public virtual async Task

 if (input.TypePerson == TypePerson.Physique)
 {
     input.Intitule = $"{input.Prenom} {input.Nom}";


 }

 if (input.Id == null)
 {
     await Create(input);

 }
 else
 {

     await Update(input);
 }

 var person = ObjectMapper.Map<Person>(input);
 var output = new GetPersonForEditOutput { Person = ObjectMapper.Map<CreateOrEditPersonDto>(person) };

 return output;

}`

And Id is here !

Please tell me if something is wrong or must be changed

Thks for All patience oguzhanagir

Bernard

Answer

Hi Bernard

As far as I understand, you want to perform a create operation using the service method and this method has a return value, which you want to receive on the Javascript side. After correcting the return value of the service method here, you can use the sample code on the Javascript side.

 _exampleService 
   .exampleMethod({ 
     id: exampleId, 
   }) 
   .done(function (result) { 
     console.log(result); 
   }); 

With this usage, you can get the values ​​returned in the service method.

Not exaxtly The personservice and the entire code have been generated by PowerTools from Asp Net zero . I just want to get the Id of the new entity because for the moment you have to close the view to be able to have this id, I would like to stay on the view and refresh by obtaining the id of the new entity. I need this id because I have one to many relationships

For example : Person has many Task

to add a new Task in Person Create view i must be able to get the PersonId.

Answer

The type seems to be good but no data returned

Answer

` public class CreatePersonOutput { public int Id { get; set; } }

[AbpAuthorize(AppPermissions.Pages_Persons_Create)] protected virtual async Task

   if (AbpSession.TenantId != null)
   {
       person.TenantId = (int?)AbpSession.TenantId;
   }

   person.Color = GenerateRandomColor();
   person.Id = 20;
   //await _personRepository.InsertAsync(person);
   //await CurrentUnitOfWork.SaveChangesAsync();

   return new CreatePersonOutput { Id = person.Id };

}

`

No changes in result after changing method

Showing 1 to 10 of 136 entries