我的目标:实现带有部分视图的网格。所以我为 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";
}
}
我的网格课是
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>
}