-3

I don't understand on how to fix the error, upon doing research on the error, it says that the error is caused when the property isn't declared in the class page. i then added declartation there and it still gives me the same error. below are the code snippets that might be related to the problem.

Severity Code Description Project File Line Suppression State Error (active) CS0120 An object reference is required for the non-static field, method, or property 'Model_Purchases.Details' Create.cshtml 175

create.cshtml

  @foreach (var detail in Model_Purchases.Details){ <--- error is in this line here
            <tr>
                <td>
                    <div class="form-group">
                        <input asp-for="@detail.Product" class="form-control" />
                        <span asp-validation-for="@detail.Product" class="text-danger"></span>
                    </div>
                </td>
                <td>
                    <div class="form-group">
                        <input asp-for="@detail.Description" class="form-control" />
                        <span asp-validation-for="@detail.Description" class="text-danger"></span>
                    </div>
                </td>

Model_Purchases.cs

namespace Accounting8.Data
{
    public class Model_Purchases
    {
        //other properties here

        public List<Model_PurchasesDetails> Details { get; set; }

        public Model_Purchases()
        {
            Details = new List<Model_PurchasesDetails>();
        }
    } 
}

create.cshtml.cs

    create.cshtml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using Accounting8.Data;
    using Microsoft.EntityFrameworkCore;
    
    namespace Accounting8.Pages.Purchases.Purchase_Requests
    {
        public class CreateModel : PageModel
        {
            private readonly Accounting8.Data.ApplicationDbContext _context;
            
            public CreateModel(Accounting8.Data.ApplicationDbContext context)
            {
                _context = context;
                Model_Purchases = new Model_Purchases();
                Model_Purchases.Details = new List<Model_PurchasesDetails>();
            }
    
            public IActionResult OnGet()
            {
                return Page();
            }
    
            [BindProperty]
            public Model_Purchases Model_Purchases { get; set; }
    
            [BindProperty]
            public Model_PurchasesDetails Model_PurchasesDetails { get; set; }
           
            [BindProperty]
            public List<Model_PurchasesDetails> Details { get; set; }
    
            public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
    
            // Add the purchase to the database
            _context.PurchasesM.Add(Model_Purchases);
            await _context.SaveChangesAsync();
    
                // Loop through the purchase details and add them to the database
                foreach (var detail in Model_Purchases.Details)
                {
                    detail.TransactionNumber = Model_Purchases.TransactionNumber;
                    _context.PurchasesDetailsM.Add(detail);
                }
                await _context.SaveChangesAsync();
    
    
            return RedirectToPage("./Index");
        }
    
        }
    }
Dale K
  • 25,246
  • 15
  • 42
  • 71

1 Answers1

1

This seems to have been caused by your naming the property after its type and the property name being interpreted as the type instead. I wouldn't have expected that to happen - it's not happening in the C# code apparently but maybe Razor uses different rules - but you should be able to force it to be interpreted as the member property rather than the type simply by qualifying the name:

@foreach (var detail in this.Model_Purchases.Details)
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • `Severity Code Description Project File Line Suppression State Error (active) CS1061 'Pages_Purchases_Purchase_Requests_Create' does not contain a definition for 'Model_Purchases' and no accessible extension method 'Model_Purchases' accepting a first argument of type 'Pages_Purchases_Purchase_Requests_Create' could be found (are you missing a using directive or an assembly reference?) – Beginner12345 Mar 08 '23 at 05:33
  • @Beginner12345, that means that the code in the first snippet is not in the same class as the code in the last snippet. What type of application is this exactly? It looks like something that I've never actually created myself and my assumptions may be incorrect. – jmcilhinney Mar 08 '23 at 05:43
  • i'm making a purchasesdetails table, i wanted to make the "details" to be dynamic. i used the CRUD scaffolding from visual studio as it's main skeleton and did some changes to it afterwards. – Beginner12345 Mar 08 '23 at 06:09
  • @Beginner12345, that's not what I meant. What project template did you select? Is it Razor Pages or Blazor or something like that? I was assuming that the first and last code snippets were part of the same class but your error messages indicate that that is not the case. – jmcilhinney Mar 08 '23 at 06:14
  • razor pages. i'm sorry for misunderstanding your question – Beginner12345 Mar 08 '23 at 06:21