如何修复“无法从'bool'Customer.Data.SelectTransactionViewModel转换”

时间:2019-01-21 04:32:56

标签: c# asp.net-mvc

我有一个如下的类和控制器

//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);
}

我已列出表中的所有数据。我想根据输入的发票编号过滤所有列表。并且在进行invoiceNumber搜索功能时出现错误。为什么我会收到此错误?请帮忙。谢谢:)

  

无法从“布尔”转换为“ CRUDExercise.ViewModel.SelectTransaksiEditorViewModel”

1 个答案:

答案 0 :(得分:0)

该错误是由于以下代码引起的:

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

第一行将对表达式求值,并以truefalse的形式返回结果,您正在将boolean的结果添加到模型中,而它期望的是SelectTransaksiEditorViewModel,因此错误。

根据@Tetsuya Yamamoto的建议,您必须提取付款清单并进行检查,否则您可以像这样重构代码:

    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);
   }