Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 视图中的泛型类型_Asp.net_Asp.net Mvc_C# 4.0_Asp.net Mvc 4 - Fatal编程技术网

Asp.net 视图中的泛型类型

Asp.net 视图中的泛型类型,asp.net,asp.net-mvc,c#-4.0,asp.net-mvc-4,Asp.net,Asp.net Mvc,C# 4.0,Asp.net Mvc 4,我的目标是:使用局部视图实现网格。所以我为Grid创建了一个类 我的代码 public class HomeController : Base_Controller { // // GET: /Home/ public ActionResult Index() { // return View("~/Views/Home/User/Login"); if (System.Web.HttpContext.Current.User.I

我的目标是:使用局部视图实现网格。所以我为Grid创建了一个类

我的代码

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";
    }
}
公共类HomeController:Base\u控制器
{
//
//回家/
公共行动结果索引()
{
//返回视图(“~/Views/Home/User/Login”);
if(System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
var users=UserBL.GetAllUsers();
Type Type=users.FirstOrDefault().GetType();
Models.Grid Grid=新Models.Grid(用户);
ViewBag.GridData=网格;
返回视图();
}
其他的
{
返回重定向操作(“登录”、“用户”);
}
//返回“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();
        }

    }
}
公共类网格
{
公共网格(对象数据源)
{
数据=(列表)数据源;
}
公共电网()
{
}
公共列表数据
{
得到;
设置
}
公共数字道具
{
得到
{
返回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)
@使用DAL.Entities;
@{
ViewBag.Title=“Index”;
}
欢迎用户。。。。!
@{Models.Grid modelgrid=(Models.Grid)@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>
@model AC.Models.Grid
@如果(型号!=null)
{
@foreach(Model.Props中的var col)
{
@Html.Display(col.Name)
}
@foreach(Model.Data中的var项)
{
@foreach(Model.Props中的var prop)
{
@Display(prop.GetValue(item,null.ToString())
}
}

}

既然您已经在使用反射,那么在这里使用泛型并没有什么好处。那么:

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();
        }
    }
}
公共类网格
{
私有只读类型;
公共网格(IEnumerable数据源)
{
如果(数据源==null)
{
抛出新的ArgumentNullException(“数据源”);
}
this.type=datasource.GetType().GetGenericArguments().SingleOrDefault();
if(this.type==null)
{
抛出新ArgumentException(“数据源必须是泛型的”);
}
this.Data=数据源;
}
公共IEnumerable数据{get;private set;}
公共数字道具
{
得到
{
返回此.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>
}
@model AC.Models.Grid
@如果(型号!=null)
{
@foreach(Model.Props中的var col)
{
@Html.Display(col.Name)
}
@foreach(Model.Data中的var项)
{
@foreach(Model.Props中的var prop)
{
@Display(prop.GetValue(item,null.ToString())
}
}
}

IEnumarable接口正确。它需要一个可测量的。假设您将其更改为Enumerable。那么,我们如何获得返回类型为(T).GetProperties().AsEnumerable()的属性;以及如何使用泛型类型“System.Collections.generic.IEnumerable”将数据分配给列表到EnumarableError需要1个类型参数在哪一行出现此错误?顺便说一句,我对我的答案做了一点修改:
private readonly Type{get;set;}
应该是
private readonly Type。无论我们在哪里使用IEnumarable。因为语法是IEnumarable。我们没有给出数据类型,我看不出您在代码中使用它的位置。但是您可以将它强制转换为底层类型:
Data.cast()…