简体   繁体   中英

How to fix "Cannot Convert from 'bool' Customer.Data.SelectTransactionViewModel"

I have a class and controller as below

//class
public class Payment
 {  
   public Guid ID { get; set; }
   public string InvoiceNumber { get; set; }
   public string ContactNumber { get; set; }
   public string ConsumerName { get; set; }
 }

 //viewmodel
 public class SelectTransaksiEditorViewModel
 {
    public Guid? Id { get; set; }
    public string InvoiceNumber { get; set; }
    public string ContractNumber { get; set; }
    public string ConsumerName { get; set; }
 }

 public class TransaksiSelectionViewModel
 {
    public List<SelectTransaksiEditorViewModel> Transactions { get; set; }
    public TransaksiSelectionViewModel()
    {
        this.Transactions = new List<SelectTransaksiEditorViewModel>();
    }
 }

 public class DataViewModel
 {
    public TransaksiSelectionViewModel transaksiSelectionViewModel { get; set; }
 }

//controller
public ActionResult Index(int? page,string searchInvoiceNumber)
{
     private DataViewModel dataViewModel = new DataViewModel();
    var model = new TransaksiSelectionViewModel();
    IPagedList<Payment> Payments= null;
    Payments = paymentRepo.GetList_Payments_InTableManualPayment(pageSize, pageNumber);

    foreach (var item in Payments )
    {
       var editorViewModel = new SelectTransaksiEditorViewModel()
       {
          Id = item.ID,
          InvoiceNumber = item.InvoiceNumber,
          ContractNumber = item.ContractNumber,
          ConsumerName = item.ConsumerName,   
       };

       if (!String.IsNullOrEmpty(searchInvoiceNumber))
       {
          var data = editorViewModel.InvoiceNumber.Contains(searchInvoiceNumber);
          model.Transactions.Add(data); //error is here
       } else {
          model.Transactions.Add(editorViewModel);
       }                   
    }
    dataViewModel.transaksiSelectionViewModel = model;
    return View(dataViewModel);
}

I have listed all the data in the table. and I want to filter all lists based on the invoice number entered. and I get an error when making the invoiceNumber search function. why am I getting this error? please help. thanks:)

cannot convert from 'bool' to 'CRUDExercise.ViewModel.SelectTransaksiEditorViewModel'

The error is due to this code:

var data = editorViewModel.InvoiceNumber.Contains(searchInvoiceNumber);
model.Transactions.Add(data);

The first line will evaluate the expression and return the result as true or false and you are adding the boolean result to your model whereas it expects SelectTransaksiEditorViewModel hence the error.

As suggested by @Tetsuya Yamamoto , you have to fetch the List of payments and put a check on it or you can refactor your code like this:

    public ActionResult Index(int? page, string searchInvoiceNumber)
    {
        private DataViewModel dataViewModel = new DataViewModel();
        var model = new TransaksiSelectionViewModel();
        IPagedList<Payment> Payments = null;
        Payments = paymentRepo.GetList_Payments_InTableManualPayment(pageSize, pageNumber);

       var editorViewModelList = new List<SelectTransaksiEditorViewModel>();
       foreach (var item in Payments )
       {
            var editorViewModel = new SelectTransaksiEditorViewModel()
            {
                Id = item.ID,
                InvoiceNumber = item.InvoiceNumber,
                ContractNumber = item.ContractNumber,
                ConsumerName = item.ConsumerName,
            };

            if (!String.IsNullOrEmpty(searchInvoiceNumber))
            {
              if (editorViewModel.InvoiceNumber.Contains(searchInvoiceNumber))
              {
                   editorViewModelList.Add(editorViewModel);
              }
            }
         }
      model.Transactions.Add(editorViewModelList);
      dataViewModel.transaksiSelectionViewModel = model;
      return View(dataViewModel);
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM