asp.net - View 中的通用类型

标签 asp.net asp.net-mvc c#-4.0 asp.net-mvc-4

我的目标:实现带有部分 View 的网格。所以我为网格创建了一个类

我的代码

public class HomeController : Base_Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        // return View("~/Views/Home/User/Login");
        if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
        {
            var users = UserBL.GetAllUsers();
            Type type = users.FirstOrDefault().GetType();
            Models.Grid<DAL.Entities.User_Details> grid = new Models.Grid<DAL.Entities.User_Details>(users);

            ViewBag.GridData = grid;
            return View();
        }
        else
        {
            return RedirectToAction("Login", "User");
        }

        // return "Hellow";
    }
}

我的网格类是

public class Grid<T> 
{
    public Grid(object datasource)
    {

        Data = (List<T>)datasource;

    }

    public Grid()
    {

    }
    public List<T> Data
    {
        get;
        set;
    }


    public IEnumerable<System.Reflection.PropertyInfo> Props
    {
        get
        {
            return typeof(T).GetProperties().AsEnumerable();
        }

    }
}

我的 View 代码是

   @using DAL.Entities;
@{
    ViewBag.Title = "Index";
}

<h1>welcome User....!</h1>
@{Models.Grid<User_Details> modelgrid = (Models.Grid<User_Details>)@ViewBag.GridData;}

@Html.Partial("GridView",modelgrid)

我的部分观点是这样的。在此代码的第一行中,我想使用任何反射机制。任何人都可以将代码更改为可行的代码

    @model AC.Models.Grid<Need a solution here>

@if (Model != null)
{
    <table>
        <thead>
            <tr>
                @foreach (var col in Model.Props)
                {

                <td>@Html.Display(col.Name)</td> 

            }
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.Data)
        {
            <tr>
                @foreach (var prop in Model.Props)
                {
                    <td>@Html.Display(prop.GetValue(item, null).ToString())</td>
                }
            </tr>
        }
    </tbody>



</table>

}

最佳答案

由于您已经在使用反射,因此在这里使用泛型没有太多好处。怎么样:

public class Grid
{
    private readonly Type type;
    public Grid(IEnumerable datasource)
    {
        if (datasource == null)           
        {
            throw new ArgumentNullException("datasource");
        }

        this.type = datasource.GetType().GetGenericArguments().SingleOrDefault();

        if (this.type == null)
        {
            throw new ArgumentException("The datasource must be generic");
        }

        this.Data = datasource;
    }

    public IEnumerable Data { get; private set; }

    public IEnumerable<System.Reflection.PropertyInfo> Props
    {
        get
        {
            return this.type.GetProperties().AsEnumerable();
        }
    }
}

然后:

@model AC.Models.Grid

@if (Model != null)
{
    <table>
        <thead>
            <tr>
                @foreach (var col in Model.Props)
                {
                    <td>@Html.Display(col.Name)</td> 
                }
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.Data)
            {
                <tr>
                    @foreach (var prop in Model.Props)
                    {
                        <td>@Html.Display(prop.GetValue(item, null).ToString())</td>
                    }
                </tr>
            }
        </tbody>
    </table>
}

关于asp.net - View 中的通用类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15926597/

相关文章:

asp.net - 负载平衡 Web 服务器上的 Orchard CMS

c# - GotFocus 和 GotKeyboardFocus 的区别

c# - 如何在 WPF 中设置 DataGrid header 的样式

c# - 如果 web.config 丢失或损坏,防止 YSOD?

c# - ASP.Net : DataPager Control always a step behind with paging

c# - 在 C# 中将字典作为参数传递给 Http.Post

asp.net - sql server 中 oracle 的 number(2,0) 的替代方案是什么?

c# - Javascript 不从隐藏的文本框读取值 - JQuery C#

asp.net - 使用 NInject 连接依赖项的选项

asp.net-mvc - 密码验证(正则表达式?)