0

Looks like I am missing a using directive or an assembly reference.

I made a CSHTML page to show a bill to user. this is made as an empty page using TBL_Bill table. but I encounter an error while trying to read data from other tables such as Tbl_Product for products. The error tells me

Tbl_Bill does not contain a definition for Tbl_Product and no accessible extension method accepting a first argument of type Tbl_Bill could be found. Are you missing a using directive or an assembly reference?

Code:

@model IEnumerable<fardashahr3.Models.Domain.Tbl_Bill>
@using fardashahr3.Models.Repository
@using fardashahr3.Models.Plugins

@{
    ViewBag.Title = "sales";
    Layout = "~/Views/Shared/_Profile.cshtml";
}

<div class="part_left">
    <div id="product_list" class="userpanel_bright_section">
        @if (ViewBag.Error != null)
        {
            <div class="alert alert-danger">
                <a class="close" style="cursor:pointer;" onclick="$('.alert').hide()">×</a>
                @ViewBag.Error
            </div>
        }

        @if (Model != null)
        {
            <h1 class="separator"> شماره فاکتور  : @Model.InvoiceNumber</h1>

            if (Model.Tbl_Product.Product_IsDownload == false)
            {
                <div class="table_style1" id="UpIBill">
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Arya
  • 91
  • 1
  • 11
  • This is not your real code, because your model is an `IEnumerable`, but you are accessing it as a single item. Also, post your Tbl_Bill class for reference. – Jesse de Wit Jul 23 '19 at 11:41
  • Thanks for your reply but what do you mean by this is not my real code? – Arya Jul 23 '19 at 12:00
  • 1
    If Model is an IEnumerable, there is no property Tbl_Product, but I'd expect the exception to say something like 'IEnumerable does not contain a definition for Tbl_Product'. – Jesse de Wit Jul 23 '19 at 12:25

2 Answers2

1

according to the error you posted, it seems your model that you are using in the view (@model IEnumerable<fardashahr3.Models.Domain.Tbl_Bill>) is not having products details (Tbl_Product) inside of it to access them using the model in your markup (Model.Tbl_Product.Product_IsDownload). Check your model properties and pass the right model into your View.

Sreekanth
  • 385
  • 1
  • 6
  • 17
1

Because your Model is an IEnumerable<...>, you need to iterate over it:

@if (Model != null)
{
    foreach (var bill in Model)
    {
        <h1 class="separator"> شماره فاکتور  : @bill.InvoiceNumber</h1>

        if (bill.Tbl_Product.Product_IsDownload == false)
        {
            ....
        }
    }
}

It's impossible to say from here if bill.Tbl_Product is the proper name to use for that property, but this should probably be enough to get you on your way.

Or if each bill has a collection of products, then you will need to do something like

foreach (var product in bill.Products)
{
}
Peter B
  • 22,460
  • 5
  • 32
  • 69