我正在vb.net中创建mvc应用程序,并且尝试使用ViewModel从SQL显示有关两个表的信息。
到目前为止,我可以成功显示一个表的信息,但是我不确定如何显示另一个表的信息。
当用户选择其ID时,将显示IssueTable,但我要与之一起显示的表是CommentsTable。
在IssueTable和CommentsTable之间存在一对多的关系,因此,例如,我想显示IssueSummary,对于每个与CommentTable中的IssueID匹配的IssueID,我想显示commentBody,这意味着每个issueSummary将具有多个注释。
到目前为止,这就是我所得到的。
我创建了一个像这样的类:
Namespace IssueTracker
Public Class IssueComments
Public cTable As CommentTable 'CommentTable in SQL database
Public iTable As IssueTable 'IssueTable in SQL database
End Class
End Namespace
然后在控制器中我得到了:
Dim IssTable As IssueTable = dbServer.IssueTables.Find(id) 'getting the ID of the selected issue
Dim iss As New IssueTable() With {
.IssueID = IssTable.IssueID,
.IssueKey = IssTable.IssueKey,
.IssueTypeID = IssTable.IssueTypeID,
.priorityID = IssTable.priorityID,
.IssueSummary = IssTable.IssueSummary,
.IssueDescription = IssTable.IssueDescription,
.Created = IssTable.Created,
.Updated = IssTable.Updated
} ' This will display the information about the specific issue crrectly
在这里,我试图显示有关评论的信息:
Dim comment As New CommentTable() With {
.CommentID = cTable.CommentID,
.CommentBody = cTable.CommentBody
} 'Will not display anything as it is not correctly connected with issueTable
Dim viewModelC As New IssueTracker.IssueComments() With {
.cTable = comment,
.iTable = iss
} 'Here I create the variable viewModel the return it in the view
Return View(viewModelC)
我正在尝试像这样连接两个表:
Dim viewModel = From c In dbServer.CommentTables Join i In dbServer.IssueTables On c.IssueID Equals i.IssueID Where (c.IssueID = i.IssueID)
Select New IssueTracker.IssueComments With {.cTable = c, .iTable = i}
如何正确显示commentTable中与IssueTable中的issueID相等的每个issueID的所有评论?
如果您希望将所有问题和评论同时发送到视图,请不要尝试在控制器中将它们组合在一起,创建一个新类(在ViewModels下),该类包含两个列表的属性,然后将它们同时发送到视图和在那里处理。
Namespace IssueTracker.ViewModels
Public Class IssueCommentsViewModel
Public Issues As List(Of Issue)
Public Comments As List(Of Comment)
End Class
End Namespace
然后,在视图中处理找到正确的数据,例如:
<table>
@For Each issue In Model.Issues
Dim comments = Model.Comments.Where(c=>c.IssueID == issue.IssueID))
@<tr>
<th>@issue.Summary</th>
<tr>
@For Each comment In comments
@<tr>
<td>@comment.CommentBody</td>
</tr>
Next
Next
<table>
(尝试制作此Razor vb.net语法-希望可以看到该概念,而不是语法问题)
编辑:填充视图模型,从问题中获取现有代码:
Dim model = New ViewModel()
model.Issues = dbServer.IssueTables
model.Comments = dbServer.CommentTables
Return View(model)
重新检查一下代码,看起来您只返回了一个问题,因此进行了更新:
Namespace IssueTracker.ViewModels
Public Class IssueCommentsViewModel
Public Issue As IssueTable
Public Comments As List(Of CommentTable)
End Class
End Namespace
Dim model = New ViewModel()
model.Issues = dbServer.IssueTables.Find(id)
model.Comments = dbServer.CommentTables.Where(x=>x.IssueID Equals id) ' this doesn't look like VB to me, so change accordingly
Return View(model)
<table>
<tr>
<th>@Model.Issue.Summary</th>
<tr>
@For Each comment In Model.Comments
@<tr>
<td>@comment.CommentBody</td>
</tr>
Next
<table>
旁白:如果您有很多问题和评论,那么您应该查看仅在视图中显示问题,然后使用ajax回调根据用户操作动态加载评论(例如,单击“展开”图标)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句