使用@Model语句时,为什么在视图页面的顶部会看到额外的文本?

时间:2014-09-05 18:44:15

标签: vb.net razor asp.net-mvc-5

VS2013,MVC5,VB,MVC MusicStore练习

我有一个View,可以使用ViewData成功显示专辑搜索的结果。 Controller的代码如下所示。 'q'是从搜索页面链接传入的搜索数据。

Function SandboxSearchResults(q As String) As ActionResult
  Dim albumQuery As System.Linq.IQueryable(Of MvcMusicStore.Album)
  albumQuery = db.Albums.Include("Artist").Where(Function(x) x.Title.Contains(q)).Take(10)
  ViewData("albumList") = albumQuery
  Return View()
End Function

视图是:

@Code
  ViewData("Title") = "SandboxSearchResults"
End Code

<h2>SandboxSearchResults</h2>

<table>
  <tr>
    <th>Artist</th>
    <th>Title</th>
    <th>Price</th>
  </tr>
@For Each A As Album In ViewData("albumList")
  @<tr>
     <td>@A.Artist.ArtistName</td>
     <td>@A.Title</td>
     <td>@A.Price</td>
   </tr>
Next
</table>

如果我只是将以下语句添加到@Code语句之前的View的开头:

@Model IEnumerable[MvcMusicStore.Models.Album]

然后,以下一行显示在我的视图顶部

IEnumerable[MvcMusicStore.Models.Album] 

如果我只将我的View调用更改为View(albumQuery),我的视图顶部会有更多内容,如下所示:

SELECT [Limit1].[AlbumId] AS [AlbumId], [Limit1].[GenreId] AS [GenreId], [Limit1].[ArtistID] AS [ArtistID], [Limit1].[Title] AS [Title], [Limit1].[Price] AS [Price], [Limit1].[AlbumArtURL] AS [AlbumArtURL], [Limit1].[ArtistID1] AS [ArtistID1], [Limit1].[ArtistName] AS [ArtistName] FROM ( SELECT TOP (10) [Extent1].[AlbumId] AS [AlbumId], [Extent1].[GenreId] AS [GenreId], [Extent1].[ArtistID] AS [ArtistID], [Extent1].[Title] AS [Title], [Extent1].[Price] AS [Price], [Extent1].[AlbumArtURL] AS [AlbumArtURL], [Extent2].[ArtistID] AS [ArtistID1], [Extent2].[ArtistName] AS [ArtistName] FROM [dbo].[Albums] AS [Extent1] INNER JOIN [dbo].[Artists] AS [Extent2] ON [Extent1].[ArtistID] = [Extent2].[ArtistID] WHERE [Extent1].[Title] LIKE @p__linq__0 ESCAPE N'~' ) AS [Limit1] IEnumerable[MvcMusicStore.Models.Album]

我传递模型的原因是为了查看将数据传输到View的其他方法。

这是从哪里来的,为什么要显示?我该怎么做才能阻止它?

1 个答案:

答案 0 :(得分:0)

您应该通过指定实际类型来引用@Model类型,如下所示:

@ModelType IEnumerable(Of MvcMusicStore.Models.Album)

Documented here