Avatar of Murray Brown
Murray Brown
Flag for United Kingdom of Great Britain and Northern Ireland asked on

ASP.net Core Create. How does the CRUD actually work?

Hi

In my ASP.net Core project I have three files. The first is the Policy.cs in the Models folder. The next is the Create.cshtml and the third is the Create.cshtml.cs . I am new to all of this so am not sure how a record gets commited to the SQL database as there is no SQL. Also if I need add a new column to the database I get that I need to add it to the the first file in the model and then the second file. What else do I need to do. The initial developer looked like she ran some sort of script


namespace WebApplication1.Model
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    public class Policy
    {
        [Key]
        public int PolicyID { get; set; }
        public string? Status { get; set; }
        public Underwriter? Underwriter { get; set; }
        [Column("Policy Number")]
        public string? PolicyNumber { get; set; }
        public bool Order { get; set; }
        [Column("Type of Risk")]
        public string? TypeOfRisk { get; set; }
        [Column("Date Seen")]
        public DateTime? DateSeen { get; set; }
        [Column("TOBA Check")]
        public bool TOBA_Check { get; set; }
        [Column("TOBA Date")]
        public DateTime? TOBA_Date { get; set; }
        [Column("Inception Date")]
        public DateTime? InceptionDate { get; set; }
        [Column("Expiry Date")]
        public DateTime? ExpiryDate { get; set; }
        [Column("Assured Name")]
        public string? AssuredName { get; set; }
        public BrokerModel? Broker { get; set; }
        [Column("Broker Contact Name")]
        public string? BrokerContactName { get; set; }
        public Country? Country { get; set; }
        [Column("State/Province")]
        public string? State_Province { get; set; }
        [Column("Fees")]
        public decimal? Fees { get; set; }
        [Column("Turnover")]
        public decimal? Turnover { get; set; }
        public string? Profession { get; set; }
        [Column("Limit of Indemnity")]
        public decimal? LimitOfIndemnity { get; set; }
        public string? Currency { get; set; }
        public string? Layer { get; set; }
        public decimal? Deductible { get; set; }
        public string? Reinstatement { get; set; }
        [Column("Costs inclusive/Excluding costs")]
        public string? CostsInclusive_ExcludingCosts { get; set; }
        [Column("Gross Premium")]
        public decimal? GrossPremium { get; set; }
        [Column("Premium of Line")]
        public decimal? PremiumOfLine { get; set; }
        [Column("Net Premium to Binder")]
        public decimal? NetPremiumtoBinder { get; set; }
        public decimal? Brokerage { get; set; }
        
        public decimal? Caytons { get; set; }
        public decimal? LCU { get; set; }
        [Column("Landmark Commission %")]
        public decimal? LandmarkCommission { get; set; }
        [Column("Total Brokerage ")]
        public decimal? TotalBrokerage { get; set; }
        [Column("Landmark Commission Amount")]
        public decimal? LandmarkCommissionAmount { get; set; }
        [Column("Broker Brokerage Amount")]
        public decimal? BrokerBrokerageAmount { get; set; }
        [Column("Caytons Brokerage Amount")]
        public decimal? CaytonsBrokerageAmount { get; set; }
        [Column("LCU Brokerage Amount ")]
        public decimal? LCUBrokerageAmount { get; set; }
        [Column("UK Policy Fee (Landmark Policy Fee)")]
        public decimal? LandmarkPolicyFee { get; set; }
        public decimal? IPT { get; set; }
        [Column("Net to Landmark")]
        public decimal? NettoLandmark { get; set; }
        [Column("Net to Landmark Inc Tax")]
        public decimal? NettoLandmarkIncTax { get; set; }
        [Column("Net to Aquliano (Excl IPT)")]
        public decimal? NettoAquliano { get; set; }
        [Column("Due Date")]
        public DateTime? DueDate { get; set; }
        [Column("Debit note issued")]
        public string? DebitNoteIssued { get; set; }
        [Column("Debit note date")]
        public DateTime? DebitNoteDate { get; set; }
        [Column("Paid Date")]
        public DateTime? PaidDate { get; set; }
        [Column("Paid Amount ")]
        public decimal? PaidAmount { get; set; }
        public decimal? Difference { get; set; }
        [Column("Bank Ref")]
        public string? BankRef { get; set; }
        [Column("Paid To Binder")]
        public decimal? PaidToBinder { get; set; }
        [Column("Paid To Aquilano Bdx  Date")]
        public DateTime? PaidToAquilanoBdxDate { get; set; }
        public string? Comments { get; set; }
        public string? Binder { get; set; }
        [Column("Class of Business")]
        public string? Class_Business { get; set; }
        public decimal? Fee { get; set; }
        [Column("Line %")]
        public decimal? Line { get; set; }
        public decimal? Tax { get; set; }
        
        public string? Email { get; set; }
        public ICollection<UploadFile> Files { get; set; }


    }

}

Open in new window


page
@model WebApplication1.Pages.Lists.CreateModel

@{
    ViewData["Title"] = "Create";
    Layout = "~/Pages/Shared/_Layout.cshtml";
}

<div class="content">
                        
    <div class="row">
        <div class="col-12">
            <div class="page-title-box">
                <div class="page-title-right">
                    <a asp-page="Index" class="btn btn-danger btn-rounded">Back List</a>
                </div>
                <h4 class="page-title">Create Policy</h4>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <form method="post">
                        <div class="row">
                            <div class="col-md-12">
                                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                            </div>
                            </div>
                        <div class="row">
                                
                            <div class="form-group  col-md-6">
                                <label  class="control-label">List Name</label>
                                <select asp-for="Item.ListName" class="form-control">
                                    <option value="Order" selected>Order</option>
                                    <option value="Type of Risk" >Type of Risk</option>
                                    <option value="Year" >Year</option>
                                    <option value="Broker" >Broker</option>
                                    <option value="Assured Name" >Assured Name</option>
                                    <option value="Country" >Country</option>
                                    <option value="CCY" >CCY</option>
                                    <option value="Profession" >Profession</option>
                                    <option value="AFF_AOC" >AFF_AOC</option>
                                    <option value="Layer" >Layer</option>
                                    <option value="Net to Aquliano(Excel IPT)" >Net to Aquliano(Excel IPT)</option>
                                    <option value="Paid To Aquliano" >Paid To Aquliano</option>
                                </select>
                            </div>
                            <div class="form-group  col-md-6">
                                <label  class="control-label">List Item</label>
                                <input asp-for="Item.ListItem" class="form-control" />
                                <span asp-validation-for="Item.ListItem" class="text-danger"></span>
                            </div>
                                
                        </div>
                        <div class="row">
                            <div class="form-group col-md-12">
                                <input type="submit" value="Create" class="btn btn-primary" />
                            </div>
                                
                        </div>
                    </form>

                </div>
            </div>
        </div>
    </div>
        
     
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Open in new window


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 WebApplication1.Data;
using WebApplication1.Model;

namespace WebApplication1.Pages.Lists
{
    public class CreateModel : PageModel
    {
        private readonly WebApplication1.Data.ApplicationDbContext _context;

        public CreateModel(WebApplication1.Data.ApplicationDbContext context)
        {
            _context = context;
        }

        public IActionResult OnGet()
        {
            return Page();
        }

        [BindProperty]
        public Item Item { get; set; }

        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _context.ListItems.Add(Item);
            await _context.SaveChangesAsync();

            return Page();
        }
    }
}

Open in new window

ASP.NETMicrosoft SQL Server

Avatar of undefined
Last Comment
Chinmay Patel

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
louisfr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Chinmay Patel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.
fblack61