2

私の目標: 部分ビューでグリッドを実装します。だから私はグリッドのクラスを作成しました

私のコード

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();
        }

    }
}

私のビューコードは

   @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>

}

4

1 に答える 1

3

すでにリフレクションを使用しているため、ここでジェネリックを使用する利点はあまりありません。どうですか:

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>
}
于 2013-04-10T13:10:12.137 に答える