使用viewModels通過MVC中的User ID在MVC中使用IEnumerable過濾行 - Filter rows using IEnumerable in MVC by the ID of User in MVC using viewModels

使用viewModels通過MVC中的User ID在MVC中使用IEnumerable過濾行 - Filter rows using IEnumerable in MVC by the ID of User in MVC using viewModels,第1张

I am creating an application in MVC in which I am displaying a list of rows in a table. I initially had it displaying all items but now what I want to do is to display certain items where the ID of the user is equal to the ID of the user in the Table.

我在MVC中創建一個應用程序,我在其中顯示表中的行列表。我最初讓它顯示所有項目,但現在我想要做的是顯示某些項目,其中用戶的ID等於表格中用戶的ID。

I had it displaying like this:

我有這樣的顯示:

In my Controller:

在我的控制器中:

Public Function IssueHistory(sortOrderProject As String, command As String, issueTable As IssueTable) As ActionResult
    Dim issues As IQueryable(Of IssueTable) = dbServer.IssueTables
    Return View(issues)
End Function

I also have it ordered in alphabetical order like this:

我也按字母順序排序,如下所示:

If sortOrderProject Is Nothing Then
     sortOrderProject = "AlphUp"
End If

Select Case sortOrderProject
    Case "AlphDown"
        ViewBag.AlphOrderIssue = "AlphUp"
        issues = issues.OrderByDescending(Function(i) i.IssueKey)
    Exit Select
    Case "AlphUp"
        ViewBag.AlphOrderIssue = "AlphDown"
        issues = issues.OrderBy(Function(i) i.IssueKey)
    Exit Select
    Case Else
        ViewBag.AlphOrderIssue = "AlphUp"
        issues = issues.OrderBy(Function(i) i.IssueKey)
    Exit Select
End Select

In my View:

在我看來:

@ModelType IEnumerable(Of IssueTable)
@Html.DisplayFor(Function(modelItem) item.IssueStatus)
@Html.DisplayFor(Function(modelItem) item.IssueStatus)

But how can I filter my application so that for each user ID in the IssueTable using a viewModel. This is what I have got so far:

但是,我如何過濾我的應用程序,以便使用viewModel為IssueTable中的每個用戶ID。這是我到目前為止所得到的:

Public Function IssueHistory(sortOrderProject As String, command As String, issueTable As IssueTracker.ClientUserProjectIssue) As ActionResult
    Dim issues As IQueryable(Of IssueTracker.ClientUserProjectIssue) = dbServer.IssueTables
    'Dim issues As IEnumerable(Of IssueTracker.ClientUserProjectIssue) = dbServer.IssueTables
    Return View(issues)
End Function

In my view:

在我看來:

@ModelType IEnumerable(Of IssueTracker.ClientUserProjectIssue)
@Html.DisplayFor(Function(modelItem) item.IssueTable.IssueStatus)
@Html.DisplayFor(Function(modelItem) item.IssueTable.IssueStatus)

My ViewModel looks like this:

我的ViewModel看起來像這樣:

Public Class ClientUserProjectIssue
    Public Property uTable As UserTable
    Public Property IssueTable As IssueTable
End Class

I tried to implement it like this but have been unsuccessful

我試圖像這樣實現它但是沒有成功

ViewModel:

視圖模型:

Public Property iEnumarableIssue As IEnumerable(Of IssueTable)

Controller:

控制器:

Dim ViewModel As IEnumerable(Of IssueTable) = dbServer.IssueTables
'ViewModel.iEnumarableIssue  = dbServer.IssueTable.Where(Function(x) x.UserID= id).ToList()

But then when I try to do it like this I get a lot of null values. What would be the correct way of filtering rows for each UserID that equals the UserID in the IssueTable using a viewModel?

但是當我嘗試這樣做時,我得到了很多空值。使用viewModel過濾每個UserID的行的正確方法是什么,它等於IssueTable中的UserID?

I can use IQueryable or IEnumerable if that is the correct way of doing it and the code can be explained in c#.

我可以使用IQueryable或IEnumerable,如果這是正確的方法,並且代碼可以在c#中解釋。

1 个解决方案

#1


1  

You should use IQueryable while you are constructing the query. When your query is ready, call ToList() to enumerate the records to the memory. Then, you can send the list to the view. Like this:

在構造查詢時應該使用IQueryable。查詢准備就緒后,調用ToList()枚舉記錄到內存中。然后,您可以將列表發送到視圖。喜歡這個:

public ActionResult IssueHistory(string sortOrderProject, int userId)
{
    var issues = dbServer.IssueTables.AsQueryable();
    //...
    issues = issues.Where(i => i.UserId == userId);
    //...
    issues = issues.OrderBy(i => i.IssueKey);
    //...
    return View(issues.ToList());
}

The Model of the view must be an IEnumerable(Of IssueTable) (not IQueryable)

視圖的模型必須是IEnumerable(Of IssueTable)(不是IQueryable)

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复