Home > Back-end >  How can I fix "Cannot convert system.generic.list to model"?
How can I fix "Cannot convert system.generic.list to model"?

Time:08-24

My error message is: "cannot convert from System.Collections.Generic.List to Model"

error message

Below code is my model:

public class PatientDemographicsandAddressModel
{
     public List<PhoneAddressModel> PhoneAddressModel { get; set; }
}

And this where I'm trying to use this List. And error comes in this code:

if (patientDemographics.PhoneAddressModel.Count > 0)
{
    patientId = patientDemographics.Id;
    // List<PhoneAddressModel> phoneAddressModel = patientDemographics.PhoneAddressModel;
    var phoneAddressModel = patientDemographics.PhoneAddressModel;
    _iPatientPhoneAddressService.SavePhoneAddress(patientId, phoneAddressModel, token, request);
}

And this is the service that I'm trying to reuse:

public JsonModel SavePhoneAddress(int patientId, PhoneAddressModel phoneAddressModel, TokenModel tokenModel,HttpRequest request)
{
    ...
}

How can I correct this issue?

CodePudding user response:

You appear to be trying to supply a List instead of a single class object to your _iPatientPhoneAddressService.SavePhoneAddress(patientId, phoneAddressModel, token, request) method. Your phoneAddressModel parameter is a PhoneAddressModel, not a List<PhoneAddressModel>.

Instead, try iterating over your list, calling your service method each time:

if (patientDemographics.PhoneAddressModel.Count > 0)
{  
   patientId = patientDemographics.Id;

   foreach (PhoneAddressModel model in patientDemographics.PhoneAddressModel)
   { 
      _iPatientPhoneAddressService.SavePhoneAddress(patientId, model, token, request);
   }
}
  • Related