Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
how to use repeated get data from table from one place by using general function take table name.

Problem:
i use repeated more statement to get data
actually i need to call it from one place if possible
but cannot do that.

so that i will not go every controller and repeat get data from reference file
i need one general place to call it by function or any other concept
how to do that if possible.

What I have tried:

on controller action e of employeecontroller
C#
var result = await _context.ReferenceFiles.Where(r => r.TableName == "Employees").ToListAsync();
ViewBag.RefList = result;

on controller actione of Itemcontroller
C#
var result = await _context.ReferenceFiles.Where(r => r.TableName == "Items").ToListAsync();
ViewBag.RefList = result;

on view
HTML
@foreach (var itemes in ViewBag.RefList)
{
  <thead>
    @itemes.FieldName
  </thead>
}
Posted
Updated 29-Dec-18 14:22pm
v2

1 solution

There are a few ways this could be done. The best thing to do would be to look at all of the options and see what works best, here are just a couple.

1. After retrieval of the various entities, map them onto a DataTable (DT). This will allow you to set the column names based on your entity properties. Return that DT as the Model and you can iterate through the Header names and then through the row values.

2. Use a "wrapper" object. The class behind this would have multiple properties of the various entities you may be using. On the view, you would use if...then logic to see which of the model.properties is not null and display accordingly.

3. You may be able to use Dynamic or Expando Objects for this; you would need to do some research on these to see if this would be able to be utilized.


The simplification of your common code issue is going to depend on how you bundle the disparate entities you are returning.
In all reality you could probably convert this to an AJAX call after the view is rendered and pass the name of the entity you want to a JsonController
 
Share this 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