简体   繁体   English

Asp.net MVC-查询多个网格

[英]Asp.net MVC - where query for multiple grids

Update 2 更新2

Thanks to both Thomas Boby and Sam C. Both had good answers and perfect solution to this problem! 感谢Thomas Boby和SamC。两个人都为这个问题提供了很好的答案和完美的解决方案!

Here's my problem: I want my view to show four grids/columns with different textboxes. 这是我的问题:我希望我的视图显示具有不同文本框的四个网格/列。 Example, when I create a Post Message I add a Priority level, 1-5, to it. 例如,当我创建发布消息时,我向其添加了1-5的优先级。

Now, I simply want to foreach every column where the "Priority = X", so the 1's shows on one column, and Posts with priority 2 shows on another column. 现在,我只想遍历“ Priority = X”的每一列,因此一列显示1,而另一列显示具有优先级2的帖子。 Do you get it? 你明白了吗? Or am I really bad at explaining this. 还是我真的不好解释这一点。

Model class: 型号类别:

 public enum Priority
{
    First = 1,
    Second = 2,
    Third = 3,
    Forth = 4,
    Fifth = 5,
}

public class Post
{
    public int postId { get; set; }

    public string postMsg { get; set; }

    public Priority priority { get; set; }
}

In my view I would want to do it like this: 在我看来,我想这样做:

    @foreach (var item in Model.Posts) // Where Model.priority = 1
    {
      <div class="col-md-2">
        <div class=" panel panel-default">
            @Html.DisplayFor(modelItem => item.postMsg, new { @class = "panel-body" })
        </div>
      </div>
    }

 @foreach (var item in Model.Posts) // Where Model.priority = 2
    {
      <div class="col-md-2">
        <div class=" panel panel-default">
            @Html.DisplayFor(modelItem => item.postMsg, new { @class = "panel-body" })
        </div>
      </div>
    }

The thing is I don't know how to make the "Where Model.priority = 1" query. 问题是我不知道如何进行“ Where Model.priority = 1”查询。 I'm not sure if I can make a query like that in the foreach ? 我不确定是否可以在foreach中进行类似的查询?

Update 更新资料

To make things much more simpler: I want to have a different "Select Where" query for different divs. 使事情变得更加简单:我想为不同的div使用不同的“选择位置”查询。

something like this ??? 像这样的东西?

@foreach( var p in Enum.GetValues( typeof( Priority) ).Cast< Priority >().OrderBy(x=>(int)x) ) //sorted from first priority to fifth
{
    @foreach (var item in Model.Posts.Where(x=>x.Priority == p)) 
    {
        <div class="col-md-2">
            <div class=" panel panel-default">
                @Html.DisplayFor(modelItem => item.postMsg, new { @class = "panel-body" })
            </div>
        </div>
    }
}

Using System.Linq something like: 使用System.Linq类似:

@foreach (var item in Model.Posts.Where(x => x.priority == Priority.First))
{
...
}

Would loop over every item with a priority of 1. 将优先级为1遍历每个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM