Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)

What I have tried:

Models classes Include HRContext

 public class ReferenceFile(have 3 key as composit keys(Code,TableName,FieldName))
    {
        public int Code { get; set; }
        public string TableName { get; set; }
        public string FieldName { get; set; }
        public string EnglishtextforLabel{ get; set; }

    }
 public class Employee
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public int EmployeeAge { get; set; }
    }
 public class HRContext : DbContext
    {
        public HRContext(DbContextOptions<HRContext> options)
: base(options)
        { }

        public DbSet<Employee> Employees { get; set; }
        public DbSet<ReferenceFile> ReferenceFiles { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<Employee>()
               .HasKey(t => new { t.EmployeeId });

            modelBuilder.Entity<ReferenceFile>()
                .HasKey(t => new { t.Code,t.TableName,t.FieldName });
        }
    }


Employee View Create(this static and i dont need . i need view labels get from reference table)

<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="EmployeeName" class="control-label"></label>
                <input asp-for="EmployeeName" class="form-control" />
                <span asp-validation-for="EmployeeName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="EmployeeAge" class="control-label"></label>
                <input asp-for="EmployeeAge" class="form-control" />
                <span asp-validation-for="EmployeeAge" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>
Posted
Updated 26-Dec-18 23:55pm

1 solution

 
Share this answer
 
Comments
ahmed_sa 29-Dec-18 2:34am    
thank you for reply
can you please if possible give me details for that
i posted again because i dont understand first post answer
i write under first post about details but no given answer for me
please any on give me more details
Richard MacCutchan 29-Dec-18 3:41am    
If you do not understand an answer then reply to the person who posted it and ask for clarification. Do not post the same question again.
ahmed_sa 29-Dec-18 4:26am    
thank you for reply
i already do as you tell me
but no answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900