2016-01-08 41 views
3

好的,所以我一直在嘗試使用一個Tuple將多個模型傳遞給一個View,並且它的工作方式就像一個魅力一樣。問題是當我嘗試這個語法錯誤,'>'在剃刀中預期''''

@model Tuple<Smart_WEB.Models.Room, List<IGrouping<string, Smart_WEB.Models.Song>>, List<IGrouping<string, Smart_WEB.Models.Video>>> 

The error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1003: Syntax error, '>' expected

Source Error:

Line 29:
Line 30:
Line 31: public class _Page_Views_Home_Index_cshtml : System.Web.Mvc.WebViewPage<Tuple<Smart_WEB.Models.Room { Line 32:
Line 33: #line hidden

如果我是放像:

@model Tuple<Smart_WEB.Models.Room, List<IGrouping<string, Smart_WEB.Models.Video>>> 

或者

@model Tuple<Smart_WEB.Models.Room, List<IGrouping<string, Smart_WEB.Models.Song>>> 

和相應的服務器端代碼工作p erfectly。

在此先感謝任何能夠幫助我的人;我即將放棄這個問題。

+1

「@ model」完全不同於您的錯誤。你確定你正在看正確的觀點嗎? – CodeCaster

+0

oops。我正在測試一些內容並複製了worng錯誤。謝謝!已經修復它。 – Jairo

+1

逗號實際上是逗號,而不是一些看起來像逗號的較高的Unicode字符? – CodeCaster

回答

2

當你基本上是傳遞多個型號在你看來和MVC設計爲一個類型傳遞給一個觀點,我強烈建議你創建一個包裝所有實體的單一模式:

public class ViewModel 
{ 
    public Smart_WEB.Models.Room Room { get; set; } 
    public List<IGrouping<string, Smart_WEB.Models.Song>> Songs { get; set; } 
    List<IGrouping<string, Smart_WEB.Models.Video>> Videos { get; set; } 
} 

然後你可以將其設置爲您的視圖中的模型:

@model ViewModel 
+2

'Tuple <>'_is_一種類型,它恰好是通用的。 MVC完全支持這一點。我同意一個視圖模型是一個更明智的方法,但OP顯示的應該只是工作。 – CodeCaster

+1

感謝您的建議,非常感謝!但更多比使用解決方法,我想知道是什麼原因造成的錯誤。 – Jairo

+2

@Jairo - 我不認爲這是一個解決方法... ViewModel是在諸如你的場景中使用的正確和正確的方法。 System.Tuple存在一些缺點和侷限性。其中之一是代碼可讀性--Model.Item1 ... Model.Item ..而不是ViewModel是自描述性的。其他缺點是System.Tuple進展不順利當你從View到Action發佈數據時......我想這不支持...... – Nirman