1
votes

I got the error from my below code

Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.List to 'System.Collections.Generic.IEnumerable<Theme_Hisaab.Models.CustomerLedger>'. An explicit conversion exists (are you missing a cast?)

Controller Code

public class CustomerLedgerController : Controller
{
    // GET: CustomerLedger
    Hisaab_ApplicationEntities7 db = new Hisaab_ApplicationEntities7();

    IEnumerable<CustomerLedger> CustomerLedger = null;
    public ActionResult Index()
    {

        CustomerLedger = (from cus_data in db.Customer_Data
                          join so in db.SalesOrder on cus_data.Customer_ID equals so.SalesOrder_ID
                          join sd in db.SalesOrderDetail on so.SalesOrder_ID equals sd.SalesOrderDetail_ID
                          select new
                          {
                              cus_data.Customer_Name,
                              cus_data.Customer_ID,
                              so.SalesOrder_ID,
                              so.FK_Customer_ID,
                              sd.SalesOrderDetail_ID,
                              sd.SalesOrder_Unit_Price,
                              sd.SalesOrder_Total,
                              sd.FK_SalesOrder_ID,
                              sd.FK_Product_ID
                          }
                           ).ToList();
        return View(CustomerLedger);
    }
}

ViewModel

public class CustomerLedger
{
    public int Customer_ID { get; set; }
    public string Customer_Name { get; set; }
    public int SalesOrder_ID { get; set; }
    public Nullable<int> FK_Customer_ID { get; set; }
    public Nullable<System.DateTime> SalesOrderDelivery_Date { get; set; }
    public string Product_Name { get; set; }
    public Nullable<int> SalesOrder_Quantity { get; set; }
    public Nullable<double> SalesOrder_Unit_Price { get; set; }
    public Nullable<double> SalesOrder_Total { get; set; }
    public int SalesOrderDetail_ID { get; set; }
    public Nullable<int> FK_SalesOrder_ID { get; set; }
    public Nullable<int> FK_Product_ID { get; set; }

    public Customer_Data Customer_Data { get; set; }
    public SalesOrderDetail SalesOrderDetail { get; set; }
    public SalesOrder SalesOrder { get; set; }
}

View

@model IEnumerable<Theme_Hisaab.Models.CustomerLedger>
....
 <table class="table">
 <tr>
     <th>
        @Html.DisplayNameFor(model => model.Customer_ID)
     </th>
     <th>
         @Html.DisplayNameFor(model => model.Customer_Name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.SalesOrder_ID)
    </th>
    ....
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Customer_ID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Customer_Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.SalesOrder_ID)
    </td>
    ....
    <td>
        @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
        @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey      */ }) |
        @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
    </td>
</tr>
}
</table>
1
Please note that the model-view-controller tag is for questions about the pattern. There is a specific tag for the ASP.NET-MVC implementation. - user3559349
Your query is creating a collection of anonymous objects (which will not work in the view). You need to create a view model containing the properties you need and project the results into a collection of that model (using select new MyViewModel { CutomerName = cus_data.Customer_Name, ... , SalesOrderID = so.SalesOrder_ID, ..... } - user3559349
thank you @StephenMuecke it works for me thank alot and for explaining the logic. - Abdullah Faraz

1 Answers

1
votes

Your list IEnumerable<CustomerLedger> CustomerLedger = null; is IEnumerable<CustomerLedger> type and also model used in view as @model IEnumerable<Theme_Hisaab.Models.CustomerLedger> so you need to pass same type in view from controller so make the query return type .AsEnumerable(). Also create and the in your select statement. At present you are selecting a anonymouse object type but you need to select CustomerLedger type. I don't know your exact database structure but I have show a way by which you can solve it

CustomerLedger = (from cus_data in db.Customer_Data
                          join so in db.SalesOrder on cus_data.Customer_ID equals so.SalesOrder_ID
                          join sd in db.SalesOrderDetail on so.SalesOrder_ID equals sd.SalesOrderDetail_ID
                          select new CustomerLedger
                          {
                              Customer_ID = cus_data.Customer_ID,
                              Customer_Name = cus_data.Customer_Name,
                              SalesOrder_ID = so.SalesOrder_ID,
                              FK_Customer_ID = so.K_Customer_ID,
                              SalesOrderDelivery_Date = so.SalesOrderDelivery_Date,
                              Product_Name = sd.SalesOrderDetail_ID,
                              SalesOrder_Quantity = sd.SalesOrderDetail_ID,
                              SalesOrder_Unit_Price = sd.SalesOrderDetail_ID,
                              SalesOrder_Total = sd.SalesOrderDetail_ID,
                              SalesOrderDetail_ID = sd.SalesOrderDetail_ID,
                              FK_SalesOrder_ID = sd.FK_SalesOrder_ID,
                              FK_Product_ID = sd.FK_Product_ID,
                              Customer_Data = sd.Customer_Data,
                              SalesOrderDetail = sd.SalesOrderDetail,
                              SalesOrder = sd.SalesOrder,
                          }).AsEnumerable();

But better way to create a ViewModel using this properties

public class MyViewModel
{
    public int CustomerID { get; set; }
    public string CustomerName { get; set; }
    public int SalesOrderID { get; set; }
    public int? FKCustomerID { get; set; }
    public int SalesOrderDetailID { get; set; }
    public double? SalesOrderUnitPrice { get; set; }
    public double? SalesOrderTotal { get; set; }
    public int? FKSalesOrderID { get; set; }
    public int? FKProductID { get; set; }
}

Then model and select statement

IEnumerable<MyViewModel> CustomerLedger = null;

select new MyViewModel
    {
        CustomerID = cus_data.Customer_ID,
        CustomerName = cus_data.Customer_Name,
        SalesOrderID = so.SalesOrder_ID,
        FKCustomerID = so.FK_Customer_ID,
        SalesOrderDetailID = sd.SalesOrderDetail_ID,
        SalesOrderUnitPrice = sd.SalesOrder_Unit_Price,
        SalesOrderTotal = sd.SalesOrder_Total,
        FKSalesOrderID = sd.FK_SalesOrder_ID,
        FKProductID = sd.FK_Product_ID
    }

and View

@model IEnumerable<Theme_Hisaab.Models.MyViewModel>