如何使用通用函数取表名或任何解决方案从一个地方重复获取表中的数据 [英] How to use repeated get data from table from one place by using general function take table name or any solution

查看:79
本文介绍了如何使用通用函数取表名或任何解决方案从一个地方重复获取表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用通用函数取表名从一个地方重复获取数据。



问题:

i使用重复更多声明来获取数据

实际上我需要从一个地方调用它,如果可能的话

但是不能这样做。



所以我不会去每个控制器并重复从参考文件中获取数据

i需要一个普通的地方来按功能或任何其他概念来调用它

如果可能的话怎么做。



我尝试过:



关于employeecontroller的控制器操作e

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

var result = await _context.ReferenceFiles.Where(r => r.TableName == "Employees").ToListAsync();
ViewBag.RefList = result;




on controller actione of Itemcontroller

var result = await _context.ReferenceFiles.Where(r => r.TableName == "Items").ToListAsync();
ViewBag.RefList = result;



查看


on view

@foreach (var itemes in ViewBag.RefList)
{
  <thead>
    @itemes.FieldName
  </thead>
}

推荐答案

有几种方法可以做到这一点。最好的办法是查看所有选项并查看最佳选择,这里只是一对。



1.检索各种实体后,将它们映射到DataTable(DT)。这将允许您根据实体属性设置列名称。将DT作为 Model 返回,您可以遍历Header名称,然后遍历行值。



2.使用包装器 对象。这背后的类将具有您可能使用的各种实体的多个属性。在视图中,您将使用if ... then逻辑来查看哪个model.properties不为null并相应显示。



3.您可以使用为此使用Dynamic或Expando对象;你需要对这些进行一些研究才能看出它是否能够被利用。





公共代码的简化问题将取决于你如何捆绑你正在返回的不同实体。

在所有现实中你可以在呈现视图后将其转换为AJAX调用并传递你想要的实体的名称到JsonController
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


这篇关于如何使用通用函数取表名或任何解决方案从一个地方重复获取表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆