By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

How to get label text from table reference on database based on Table Name

P: 4
SummaryProblem
=============

How to get label text from table reference on database based on TableName and FieldName dynamically from database and show on view createEmployee .

Details Problem

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 .

===============Goal From I post

I need function or any thing general let me get text of label from database because Title of labels on my suituation changed more)

===============Tools Used

Tools used sql server 2012 and visual studio 2017 asp.net core 2.1


Database Have two tables Employee and Reference File(may be increase models and views so that if any thing general is prefer)
====Code==========
Query Get Data from reference Table

Expand|Select|Wrap|Line Numbers
  1. SELECT TableName, FieldName,EnglishtextforLabel FROM ReferenceFile WHERE (FieldName = 'EmployeeId' ) AND TableName = 'Employee'


Expand|Select|Wrap|Line Numbers
  1. Models classes Include HRContext
  2.  
  3.  public class ReferenceFile(have 3 key as composit keys(Code,TableName,FieldName))
  4.     {
  5.         public int Code { get; set; }
  6.         public string TableName { get; set; }
  7.         public string FieldName { get; set; }
  8.         public string EnglishtextforLabel{ get; set; }
  9.  
  10.     }
  11.  public class Employee
  12.     {
  13.         public int EmployeeId { get; set; }
  14.         public string EmployeeName { get; set; }
  15.         public int EmployeeAge { get; set; }
  16.     }
  17.  public class HRContext : DbContext
  18.     {
  19.         public HRContext(DbContextOptions<HRContext> options)
  20. : base(options)
  21.         { }
  22.  
  23.         public DbSet<Employee> Employees { get; set; }
  24.         public DbSet<ReferenceFile> ReferenceFiles { get; set; }
  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========
Sample data for ReferenceTable

Expand|Select|Wrap|Line Numbers
  1. Code  TableName  FieldName    EnglishtextforLabel 
  2. 1      Employee  EmployeeId          Code
  3. 2      Employee  EmployeeName        Name        
  4. 3      Employee  EmployeeAge         Age
================
============Result I need==========

labels on view create must be Give me the Result below :

Code

Name

Age
================
Dec 28 '18 #1
Share this Question
Share on Google+
3 Replies


P: 4
can any one help me answer question above
Dec 29 '18 #2

P: 5
try this code for the C#
2 Weeks Ago #3

P: 5
Find the attachment for the code
Attached Files
File Type: docx code.docx (11.9 KB, 3 views)
2 Weeks Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.