When saving always the value [ OnPost() ] null [ InvoiceDetails ]

Posted by: magdyabaza on 14 June 2023, 2:23 pm EST

  • Posted 14 June 2023, 2:23 pm EST

    **** When saving always the value [ OnPost() ] null [ InvoiceDetails ]

    // Create the data models: Define two data model classes representing the invoice header and detail
    public class Invoice
    {
        public int Id { get; set; }
        public string InvoiceNumber { get; set; }
        public DateTime InvoiceDate { get; set; }
        // Other invoice properties
        
        public List<InvoiceDetail> InvoiceDetails { get; set; }
    }
    
    public class InvoiceDetail
    {
        public int Id { get; set; }
        public int InvoiceId { get; set; }
        public string ProductName { get; set; }
        public decimal Quantity { get; set; }
        public decimal Price { get; set; }
        // Other detail properties
    }
    
    // Create the Razor Page: Add a Razor Page (Create.cshtml.cs) to handle the invoice creation and data saving.
    
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using System.Collections.Generic;
    
    namespace YourNamespace.Pages.Invoices
    {
        public class CreateModel : PageModel
        {
            [BindProperty]
            public Invoice Invoice { get; set; }
    
            public void OnGet()
            {
                Invoice = new Invoice();
                Invoice.InvoiceDetails = new List<InvoiceDetail>();
            }
    
            public void OnPost()
            {
                if (!ModelState.IsValid)
                    return Page();
    
                // Save the invoice and its details to the database or perform other necessary actions
    		
                return RedirectToPage("Index");
            }
        }
    }
    
    // Create the Razor Page view: Add a Razor Page view (Create.cshtml) for the invoice creation form.
    @page
    @model YourNamespace.Pages.Invoices.CreateModel
    @{
        ViewData["Title"] = "Create Invoice";
    }
    
    <h2>@ViewData["Title"]</h2>
    
    <form method="post">
        <div class="form-group">
            <label asp-for="Invoice.InvoiceNumber"></label>
            <input class="form-control" asp-for="Invoice.InvoiceNumber" />
            <span asp-validation-for="Invoice.InvoiceNumber" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Invoice.InvoiceDate"></label>
            <input type="date" class="form-control" asp-for="Invoice.InvoiceDate" />
            <span asp-validation-for="Invoice.InvoiceDate" class="text-danger"></span>
        </div>
    
        <h3>Invoice Details</h3>
        <div id="flexGrid"></div>
        <div class="container">
             <div>
                  <c1-flex-grid id="flexGrid" auto-generate-columns="false" class="grid" is-read-only="false" allow-add-new="true" allow-delete="true" selection-mode="Row" allow-sorting="true" height="200px">
                  <c1-flex-grid-column asp-for="Invoice.InvoiceId" binding="Acc_No" header="Product Code"></c1-flex-grid-column>
                  <c1-flex-grid-column asp-for="Invoice.ProductName" binding="Product Name" header="Account Name"></c1-flex-grid-column>
                  <c1-flex-grid-column asp-for="Invoice.Quantity" binding="Quantity" header="Cost"></c1-flex-grid-column>
                  <c1-flex-grid-column asp-for="Invoice.Price" binding="Price" header="Notice"></c1-flex-grid-column>
                  <c1-items-source page-size="10" source-collection="@Model.Invoice.InvoiceDetails"></c1-items-source>   
                </c1-flex-grid>
             </div>
        </div>	
        <button type="submit" class="btn btn-primary">Save</button>
    </form>
    
  • Posted 14 June 2023, 10:59 pm EST

    Hi,

    Please refer to the following sample which demonstrates how you could send Grid data to the server inside a form.

    Regards,

    Avinash

    RAZor_Forms.zip

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels