1 Replies - 297 Views - Last Post: 15 February 2019 - 05:45 PM Rate Topic: -----

#1 ahmedba   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 24-January 14

How to display data for label from database and when input type is hid

Posted 14 February 2019 - 07:37 PM

Problem

How to display data for label from database on view and when input type is hidden not display data for this control or property from database ?

1- suppose I have model Employee as following

Code:
public class Employee
    {
[key]
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
    }

2- i create reference file table and his model on project and this data on table referencefile

Code:
code	TableName	FieldName
1	Employees	EmployeeId
2	Employees	EmployeeName


when call method

Code:
@await Component.InvokeAsync("GetReference", new { TableName = "Employees" })

it show all data from reference file table success but if input type is hidden it show also data

<input type="hidden" asp-for="EmployeeId" />

in case of EmployeeId is hidden must not show EmployeeId meaning result must show

EmployeeName only

I have view component return list of data based on table name Reference File

what i need when control or property is hidden on view call function invokeasync show data not hidden on view and exist on database

How to do that please ?

pseudo code

if(field exist on reference file table && is not hidden on view ui

show it as label text

else

not display it

what I have Try

Code Details of view component

Code:
public  class GetReferenceViewComponent : ViewComponent
    {
        private readonly TabDbContext _context;

        public GetReferenceViewComponent(TabDbContext context)
        {
            _context = context;
        }

        public async Task<IViewComponentResult> InvokeAsync(string TableName)
        {
           
            var result = _context.ReferenceFiles.Where(r => r.TableName == TableName).ToList();
            ViewBag.GetReference = result;
            return View();
        }


on view component view shared/getreference/Default.cshtml

@foreach (var itemes in ViewBag.GetReference)
    {

        <ul>
            <li> @itemes.FieldName </li>
        </ul>
    }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: How to display data for label from database and when input type is hid

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5378
  • View blog
  • Posts: 14,286
  • Joined: 18-April 07

Re: How to display data for label from database and when input type is hid

Posted 15 February 2019 - 05:45 PM

And why can't you use an if statement in your cshtml file view? Razor supports if statements...

@foreach (var itemes in ViewBag.GetReference)
{
   if (itemes.FieldName != String.Empty) {
        <label>@itemes.FieldName</label>
   } 

   // Do whatever else with itemes...
}



Here we are simply checking the existence of a value and printing the label if it is not an empty string. You can of course do any check you like prior to the view rendering to the user. But once you send a value out to the user, then it is in the realm of hiding/showing based on client side code (Javascript)

Hopefully that is what you are looking to do. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1