ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 278.3k

How to get label text from table reference on database base

Dec 27 2018 2:59 AM
Problem
How to get label text from table reference on database based on TableName and FieldName dynamically from database
 
and show on view createEmployee .
 
Meaning i need to get label text dynamically from database not static from model
 
SO that Every Time i need to change text of label i will change it from database depend on reference table
 
AND No need to change from code .
 
IF You give me function or any thing general because i have more models and view i need to make like that)
 
Tools used sql server 2012 and visual studio 2017 asp.net core 2.1
 
Query Get Data from reference Table
 
SELECT TableName, FieldName,EnglishtextforLabel FROM ReferenceFile WHERE (FieldName = 'EmployeeId' ) AND TableName = 'Employee'
 
Database Have two tables Employee and ReferenceFileDatabase Have two tables Employee and Reference File(may be
 
increase
 
 
models and views so that if any thing general is prefer)
 
    1. Models classes Include HRContext  
    2.   
    3.  public class ReferenceFile(have 3 key as composit keys(Code,TableName,FieldName))  
    4.     {  
    5.         public int Code { getset; }  
    6.         public string TableName { getset; }  
    7.         public string FieldName { getset; }  
    8.         public string EnglishtextforLabel{ getset; }  
    9.   
    10.     }  
    11.  public class Employee  
    12.     {  
    13.         public int EmployeeId { getset; }  
    14.         public string EmployeeName { getset; }  
    15.         public int EmployeeAge { getset; }  
    16.     }  
    17.  public class HRContext : DbContext  
    18.     {  
    19.         public HRContext(DbContextOptions<HRContext> options)  
    20. base(options)  
    21.         { }  
    22.   
    23.         public DbSet<Employee> Employees { getset; }  
    24.         public DbSet<ReferenceFile> ReferenceFiles { getset; }  
    25.         protected override void OnModelCreating(ModelBuilder modelBuilder)  
    26.         {  
    27.             base.OnModelCreating(modelBuilder);  
    28.             modelBuilder.Entity<Employee>()  
    29.                .HasKey(t => new { t.EmployeeId });  
    30.   
    31.             modelBuilder.Entity<ReferenceFile>()  
    32.                 .HasKey(t => new { t.Code,t.TableName,t.FieldName });  
    33.         }  
    34.     }  
    35.   
    36.   
    37. Employee View Create(this static and i dont need . i need view labels get from reference table)  
    38.   
    39. <div class="row">  
    40.     <div class="col-md-4">  
    41.         <form asp-action="Create">  
    42.             <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
    43.             <div class="form-group">  
    44.                 <label asp-for="EmployeeName" class="control-label"></label>  
    45.                 <input asp-for="EmployeeName" class="form-control" />  
    46.                 <span asp-validation-for="EmployeeName" class="text-danger"></span>  
    47.             </div>  
    48.             <div class="form-group">  
    49.                 <label asp-for="EmployeeAge" class="control-label"></label>  
    50.                 <input asp-for="EmployeeAge" class="form-control" />  
    51.                 <span asp-validation-for="EmployeeAge" class="text-danger"></span>  
    52.             </div>  
    53.             <div class="form-group">  
    54.                 <input type="submit" value="Create" class="btn btn-default" />  
    55.             </div>  
    56.         </form>  
    57.     </div>  
    58. </div>  

 
Here i need to get label on view create from table reference not static field name
Sample data for ReferenceTable
  1. Code  TableName  FieldName    EnglishtextforLabel   
  2. 1      Employee  EmployeeId          Code  
  3. 2      Employee  EmployeeName        Name          
  4. 3      Employee  EmployeeAge         Age  
 
As reference table above sample labels on view create must be Give me the Result below:
 
Code
Name
Age
 
 

Answers (3)