1

I'm trying to import data from excel using LinqToExcel. I have few readonly properties in the model class. When I try to map them with the excel columns, they fail with following error. Also when I drop those columns from excel, it works fine without values.

Method 'Total' not found.

Model:Budget

    [Required]
    [Display(Name = "Room Type")]
    public int RoomTypeID { get; set; }
    [ForeignKey("RoomTypeID")]
    public virtual RoomType RoomType { get; set; }

    public decimal Pair { get; set; }
    [ExcelColumn("Cost per Room*")]
    public decimal CostPerRoom { get; set; }

    [NotMapped]
    [ExcelColumn("Total")]
    [Display(Name = "Total")]
    public decimal Total
    {
        get
        {
            if (this.RoomType != null)
            {
                return this.CostPerRoom * this.RoomType.RoomTypeQty * this.Pair;
            }
            else
            {
                return 0;
            }

        }
    }

Budget Controller:

    public ActionResult ReadFromExcel()
    {
        var file = Request.Files[0];
        if (file != null && file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/Uploads/"), fileName);
            file.SaveAs(path);


            var excel = new ExcelQueryFactory(path);
            excel.DatabaseEngine = DatabaseEngine.Ace;

            excel.TrimSpaces = LinqToExcel.Query.TrimSpacesType.Both;

            var budgets = from c in excel.Worksheet<Budget>("WorksheeName") select c;
            foreach (var item in budgets) // This is where it generates the error.
            {
            }
      }

How do I overcome this?

ecasper
  • 489
  • 1
  • 10
  • 30

0 Answers0