I have UsersClass wich never be more then 10 items. How i can display user class with table in view something like this?
我有UsersClass wich超过10项。如何在视图中显示带有表的用户类?
I want to show two rows with 10 cells
我想要显示有10个单元格的两行
2 个解决方案
#1
10
As always and in every ASP.NET MVC application I recommend using a view model which is adapted to the requirements of the view. So in this view you mentioned a table in which you want to group those users by 5 elements per row:
一如既往,在每一个ASP。NET MVC应用程序我推荐使用一个适合视图需求的视图模型。在这个视图中,你提到了一个表格你想把这些用户按每行5个元素进行分组:
public class MyViewModel
{
public int Key { get; set; }
public IEnumerable<UsersClass> Users { get; set; }
}
and then in the controller action:
然后在控制器动作中:
public ActionResult Index()
{
// that's your domain model => a list of UsersClass
// could be coming from wherever you want
var users = Enumerable.Range(1, 7).Select(x => new UsersClass
{
Id = x
});
// Now let's group those users into the view model:
// We will have 5 elements per row
var model = users
.Select((u, index) => new { User = u, Index = index })
.GroupBy(x => x.Index / 5)
.Select(x => new MyViewModel
{
Key = x.Key,
Users = x.Select(y => y.User)
});
return View(model);
}
and finally the strongly typed view is pretty trivial:
最后,强类型视图非常简单
@model IEnumerable<MyViewModel>
<table>
@foreach (var item in Model)
{
<tr>
@foreach (var user in item.Users)
{
<td>@user.Id</td>
}
<!-- That's just to fill the row with the remaining
td if we have less than 5 users. Obviously
depending on your requirements you could use
a single td with a calculated colspan or something else
-->
@for (int i = item.Users.Count(); i < 5; i++)
{
<td></td>
}
</tr>
}
</table>
Obviously this view could be simplified even further using display templates:
显然,使用显示模板可以进一步简化这个视图:
@model IEnumerable<MyViewModel>
<table>
@Html.DisplayForModel()
</table>
and in the corresponding display template:
在相应的显示模板中:
@model MyViewModel
<tr>
@Html.DisplayFor(x => x.Users)
@for (int i = item.Users.Count(); i < 5; i++)
{
<td></td>
}
</tr>
and the UsersClass display template:
和UsersClass显示模板:
@model UsersClass
<td>@user.Id</td>
and the result:
结果:
#2
3
Not sure if this is exactly what you're looking for, but if it's not exact, you should be able to modify it easily:
不确定这是否是你想要的,但如果不准确,你应该能够很容易地修改:
@* Check to make sure your user class is not null *@
@if (Model.UserClass != null)
{
<table>
for (int i = 1; i <= 2; i++)
{
<tr>
for (int j = 1; j <= 5; j++)
{
<td>if (Model.UserClass[(j*i)-1] != null) { Model.UserClass[(j*i)-1] }</td>
}
</tr>
}
</table>
}
I wrote this pretty quickly, but this should be close to what you need.
我写得很快,但这应该接近你需要的。
#1
10
As always and in every ASP.NET MVC application I recommend using a view model which is adapted to the requirements of the view. So in this view you mentioned a table in which you want to group those users by 5 elements per row:
一如既往,在每一个ASP。NET MVC应用程序我推荐使用一个适合视图需求的视图模型。在这个视图中,你提到了一个表格你想把这些用户按每行5个元素进行分组:
public class MyViewModel
{
public int Key { get; set; }
public IEnumerable<UsersClass> Users { get; set; }
}
and then in the controller action:
然后在控制器动作中:
public ActionResult Index()
{
// that's your domain model => a list of UsersClass
// could be coming from wherever you want
var users = Enumerable.Range(1, 7).Select(x => new UsersClass
{
Id = x
});
// Now let's group those users into the view model:
// We will have 5 elements per row
var model = users
.Select((u, index) => new { User = u, Index = index })
.GroupBy(x => x.Index / 5)
.Select(x => new MyViewModel
{
Key = x.Key,
Users = x.Select(y => y.User)
});
return View(model);
}
and finally the strongly typed view is pretty trivial:
最后,强类型视图非常简单
@model IEnumerable<MyViewModel>
<table>
@foreach (var item in Model)
{
<tr>
@foreach (var user in item.Users)
{
<td>@user.Id</td>
}
<!-- That's just to fill the row with the remaining
td if we have less than 5 users. Obviously
depending on your requirements you could use
a single td with a calculated colspan or something else
-->
@for (int i = item.Users.Count(); i < 5; i++)
{
<td></td>
}
</tr>
}
</table>
Obviously this view could be simplified even further using display templates:
显然,使用显示模板可以进一步简化这个视图:
@model IEnumerable<MyViewModel>
<table>
@Html.DisplayForModel()
</table>
and in the corresponding display template:
在相应的显示模板中:
@model MyViewModel
<tr>
@Html.DisplayFor(x => x.Users)
@for (int i = item.Users.Count(); i < 5; i++)
{
<td></td>
}
</tr>
and the UsersClass display template:
和UsersClass显示模板:
@model UsersClass
<td>@user.Id</td>
and the result:
结果:
#2
3
Not sure if this is exactly what you're looking for, but if it's not exact, you should be able to modify it easily:
不确定这是否是你想要的,但如果不准确,你应该能够很容易地修改:
@* Check to make sure your user class is not null *@
@if (Model.UserClass != null)
{
<table>
for (int i = 1; i <= 2; i++)
{
<tr>
for (int j = 1; j <= 5; j++)
{
<td>if (Model.UserClass[(j*i)-1] != null) { Model.UserClass[(j*i)-1] }</td>
}
</tr>
}
</table>
}
I wrote this pretty quickly, but this should be close to what you need.
我写得很快,但这应该接近你需要的。