Filter strings using IEnumerable in MVC using user id in MVC using viewModels

I am creating an application in MVC in which I show a list of rows in a table. At first I displayed all the elements, but now I want to show some elements in which the user ID is equal to the user ID in the table.

I had a mapping 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 ordered it in alphabetical order as follows:

 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 IssueTable using viewModel. This is what I got so far:

 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 opinion:

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

My ViewModel looks like this:

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

I tried to implement it in this way, but was 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 this, I get a lot of null values. What would be the proper way to filter the rows for each UserID that is equal to the UserID in the IssueTable using the viewModel?

I can use IQueryable or IEnumerable if this is the right way to do this and the code can be explained in C #.

+4
source share
1 answer

You must use IQueryable during query building. When your request is ready, call ToList () to list the entries in memory. Then you can send the list to the view. Like this:

 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()); } 

Model view Model must be IEnumerable(Of IssueTable) (not IQueryable)

+1
source

All Articles