我在asp.net MVC razor视图中遇到了一个奇怪的问题。
我想要我的模型是一个List<Tuple<string, int, int, int, int>>
,这在我的其他c#方法中完全有效。但当我把它粘贴到@model
声明中时,它似乎只选择元组的字符串部分。因此我没有整数。只有item1。
如果我让它绑定到一个元组而不是列表,这个问题就不存在了。
看起来生成的代码是错误的,所以这可能是razor视图中的一个bug吗?
编译时出现的错误是:
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 27:
Line 28:
Line 29: public class _Page_Views_Dashboard_DestinationStatistics_cshtml : System.Web.Mvc.WebViewPage<List<Tuple<string {
Line 30:
Line 31: #line hidden
为了分离这个问题,我做了以下的事情:
创建一个空的asp.net mvc项目。创建一个新视图。粘贴以下代码。
@model List<Tuple<string, int, int, int, int>>
@foreach (var stat in Model)
{
<tr>
<td>
@stat.Item1
</td>
<td>
@stat.Item2
</td>
<td>
@stat.Item3
</td>
<td>
@stat.Item4
</td>
<td>
@stat.Item5
</td>
</tr>
}
我知道我可以创建一个类或结构体来保存数据。只是出于好奇问一下。
编辑: 已解决并在这里向MVC团队报告 http://aspnet.codeplex.com/workitem/8652
public class _Page_Views_Home_Index_cshtml : System.Web.Mvc.WebViewPage<global::System.Collections.Generic.List<global::System.Tuple<string {
(我已经尝试使用global::
前缀来让它工作)。看起来 Razor 解析@model
指令在第一个泛型参数后失败了。 - Andras ZoltanCodeTypeReference
中存在一个错误 - 请参见我的更新(现在已经修剪过)的答案。 - Andras Zoltan@model List<Tuple<string, int, int, int, int>>
,那么现在是时候使用视图模型类了,这样可以避免让其他程序员在理解你的代码时头疼不已!在这种情况下,元组是一种代码异味。 - Tim Abell