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

ASP.net Core Project questions


I an ASP.net Core page called Create.cshtml with the code below. I also have a class further on that refers to a table called ListItems and another one that refers to a table called DataEntryEntry. I take it that the line  @foreach (var item in Model.ListControls)

refers to the table ListItems but how does the system know this

Also in the following line

                        if (item.Visible == true && item.Column == "Policy Number")

This refers to the Table DataEntryControl but how does it know that the system refers to this table. Is it simply because these classes are in the folder "Model" as shown in the image? Also finally, I take it that the page Create.cshtml in the folder Pages/Policies is a name that will automatically be given for CRUD. I am really just trying to work out how things liknk up.


@model WebApplication1.Pages.Policies.CreateModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @{     ViewData["Title"] = "Create";     Layout = "~/Pages/Shared/_Layout.cshtml"; } <div class="content">     <div class="row">         <div class="col-6">             <div class="page-title-box" style="margin:20px">                 <a asp-page="Index" class="btn btn-danger btn-rounded">Back List</a>             </div>         </div>         <div class="col-6">             <div class="page-title-box" style="margin:20px">                 <form method="post">                 </form>             </div>         </div>     </div>     <div class="row">         <div class="col-12">             <p>@Model.errorMessage</p>         </div>     </div>     <div class="row">         <div class="col-12">             <div class="card">                 <div class="card-body">                     <form method="post" enctype="multipart/form-data">                         <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 asp-for="Policy.Status" class="control-label"></label>                                 <select asp-for="Policy.Status" asp-items="Model.Status" class="form-control" onchange="this.form.submit()" id="Status">                                 </select>                                 <span asp-validation-for="Policy.Status" class="text-danger"></span>                             </div>                             @foreach (var item in Model.ListControls) //The C# code marked with @ is executed on the server before the page is sent to the client                             {                                 if (item.Visible == true && item.Column == "Policy Number")                                 {                                     <div class="form-group  col-md-6">                                         <label asp-for="Policy.PolicyNumber" class="control-label"></label>                                         @if (item.Enabled == true && item.Mandatory == true)                                         {                                             <input asp-for="Policy.PolicyNumber" required class="form-control" id="PolicyNumber" />                                         }                                         else if (item.Enabled == true && item.Mandatory == false)                                         {                                             <input asp-for="Policy.PolicyNumber" class="form-control" id="PolicyNumber" />                                         }                                         else if (item.Enabled == false && item.Mandatory == true)                                         {                                             <input asp-for="Policy.PolicyNumber" disabled required class="form-control" id="PolicyNumber" />                                         }                                         else                                         {                                             <input asp-for="Policy.PolicyNumber" disabled class="form-control" id="PolicyNumber" />                                         }                                         <span asp-validation-for="Policy.PolicyNumber" class="text-danger"></span>                                     </div>                                 }

Open in new window

namespace WebApplication1.Model
    public class Item     {         public int ID { get; set; }         public string ListName { get; set; }         public string ListItem { get; set; }     } }

Open in new window

using System;
using System.Collections.Generic;
using System.Linq; using System.Threading.Tasks; namespace WebApplication1.Model {     public class DataEntryControl     {         public int ID { get; set; }         public string Status { get; set; }         public string Table { get; set; }         public string Column { get; set; }         public bool Visible { get; set; }         public bool Enabled { get; set; }         public bool Mandatory { get; set; }         public string ValidationMessage { get; set; }     } }

Open in new window


Avatar of undefined
Last Comment

8/22/2022 - Mon

The Model variable contains an instance of the class marked at the top of the page with @model. It's  WebApplication1.Pages.Policies.CreateModel.
If you put the mouse over Model, a tooltip should appear telling you what the class is. Likewise, if you put it over ListControls, you should see the definition of the ListControls property, in the CreateModel class.
If you click on ListControls and press F12, you should be sent to the property's definition.
Murray Brown

thanks. In the file Create.cshtml I only see one line with @model that doesn't contain any reference to a cast
    <div class="row">
        <div class="col-12">


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
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes