大家早上好。
我是.Net语言的初学者,需要一个能够更进一步的例子。
因此,我的目标是在数据表中显示日期和注释,下面是关于客户的一般信息。
视图需要成为类似的东西:
姓名: 地址: 电话号码 : ...
DATE1 注释1
DATE2 注释2 ...
使用常规数据自动生成强类型视图很容易。现在,我不知道如何显示以下评论。
以下是我在控制器中所做的事情
' GET: /Contacts/Details/5
Function Details(id As Integer) As ActionResult
Dim contact As contact = db.contact.Single(Function(c) c.idContact = id)
Dim listMeet = New List(Of meeting)
listMeet = (From d In db.meeting
Where d.FK_meet_contact = id
Select d).ToList()
ViewBag.listeMeeting = listMeet
Return View(contact)
End Function
在视图中,我有很多错误的东西......让我们告诉你最后一个:
@ModelType MvcApplication4.contact
@Code
ViewData("Title") = "Details"
Dim list As List(Of Object) = ViewBag.listeMeeting
Dim ligne As ListItemCollection
End Code
[...]
<fieldset>
<legend><button onclick="togglefield('Meet')">Meetings</button></legend>
<div class="Meet">
@For Each ligne In ViewBag.listeMeeting
@Html.Raw(ViewBag.listeMeeting)
Next (ligne)
</div>
</fieldset>
什么还没有我很好理解?
ps:我不是母语为英语的人,所以,抱歉,如果我的英语很糟糕答案 0 :(得分:1)
您可以使用视图模型而不是ViewBag
:
Public Class MyViewModel
Public Property ContactDetails As Contact
Public Property Meetings As IEnumerable(Of Meeting)
End Class
然后在控制器中填充此视图模型并传递给视图以显示:
Function Details(id As Integer) As ActionResult
Dim contact As contact = db.contact.Single(Function(c) c.idContact = id)
Dim meetings =
(From d In db.meeting
Where d.FK_meet_contact = id
Select d).ToList()
Dim model = New MyViewModel With {
.ContactDetails = contact,
.Meetings = meetings
}
Return View(model)
End Function
然后将您的视图强烈输入到视图模型中:
@ModelType AppName.MyViewModel
<h2>@Model.ContactDetails.SomePropertyOfContact</h2>
<fieldset>
<legend>
<button onclick="togglefield('Meet')">Meetings</button>
</legend>
<div class="Meet">
@For Each meeting In Model.Meetings
@meeting.SomePropertyOfMeeting
Next
</div>
</fieldset>