asp.net mvc 3 剃刀视图 ->元组问题的强类型列表

本文介绍了asp.net mvc 3 剃刀视图 ->元组问题的强类型列表的处理方法,对大家解决问题具有一定的参考价值

问题描述

我在使用 asp.net MVC 剃刀视图时遇到了一个奇怪的问题.

I'm having an odd problem with asp.net MVC razor view.

我希望我的模型是一个 List<tuple></tuple 这在我的其他 c# 方法中完全有效.但是当我将它粘贴到 @model 声明中时,它似乎只挑选出元组的字符串部分.所以我没有整数.只有 item1.

I want my model to be a List<Tuple<string, int, int, int, int>> which is perfectly valid in my other c# methods. But when I paste it into the @model declaration it seems to only pick out the string part of the tuple. So I have no ints. Only item1.

如果我将它绑定到元组而不是列表,则不会出现此问题.

This problem is not present if I make it bind to a Tuple instead of the List.

好像生成的代码有问题,所以这可能是剃刀视图中的一个错误?

Seems like the generated code is wrong, so perhaps this is a bug in razor view?

我在编译时遇到的错误是:

The error I get at compilation is:

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

为了隔离这个问题,我做了以下事情:

To isolate this problem I did the following thing:

创建一个空的 asp.net mvc 项目.创建一个新视图.粘贴以下代码.

Create an empty asp.net mvc project. Create a new view. Past the following code.

@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>
}

我知道我可以创建一个类或一个结构来保存数据.只是出于好奇而问

I know I can just create a class or a struct to hold the data instead. Just asking out of curiosity

在此处解决并报告给 MVC 团队 http://aspnet.codeplex.com/workitem/8652

Solved and reported to the MVC team here http://aspnet.codeplex.com/workitem/8652

推荐答案

EDIT 我在这里删减了一些正在进行的评论 - 只需查看历史记录即可.

因此,您可以使用 1、2、3 或 4 元组通用参数进行此操作,但不适用于 5.一旦您使用 5 个参数,它就会生成如下代码:

So you can make this work with 1, 2, 3 or 4 tuple generic parameters but it doesn't work with 5. As soon as you use 5 parameters it generates code like this:

 public class _Page_Views_Home_Index_cshtml : 
   System.Web.Mvc.WebViewPage<List<System.Tuple<string {

我只想知道它是否是字符长度限制,所以我生成了一个这样的类:

I wanted to just find out if it's a character-length limitation, so I generated a class like this:

namespace ASP{  //same namespace that the backend code for the page is generated
  public class T { } 
}

并更改了模型声明:

@model List<Tuple<T,T,T,T,T>>.

最后(见历史)我得到了

In the end (see the history) I got to

@inherits System.Web.Mvc.WebViewPage<Tuple<T,T,T,T,T>>

同样的问题!@model关键字没有问题...

Same problem! It's not a problem with the @model keyword...

花了一些时间(通读 MVC3 和 Razor 源代码,向该解决方案添加了一些测试) - 但这里有一个测试说明了我们为什么会收到此错误:

It took a while (reading through the MVC3 and Razor source, adding a couple of tests to that solution) - but here's a test that shows the why we get this error:

[TestMethod]
public void TestMethod()
{
  System.CodeDom.CodeTypeReferenceCollection c = 
    new CodeDom.CodeTypeReferenceCollection();
  c.Add("Tuple<T,T,T,T>");
  c.Add("Tuple<T,T,T,T,T>");
  //passes
  Assert.AreEqual("Tuple<T,T,T,T>", c[0].BaseType);
  //fails
  Assert.AreEqual("Tuple<T,T,T,T,T>", c[1].BaseType);    
}

所以 - 四参数版本通过,但没有 5 参数版本.

So - the four-parameter version passes, but not the 5 parameter version.

猜猜看 - 实际值是 Tuple<t - 即截断的泛型类型名称 截断方式与您在代码中观察到的完全相同.</t

And guess what- the actual value is Tuple<T - i.e. a truncated generic type name truncated exactly the same way that you've observed in your code.

标准 Razor 解析器和 Mvc Razor 解析器在解析 @inherits@model 关键字时都使用 CodeTypeReferenceCollection 类型.下面是代码生成过程中 @inherits 的代码:

Both the standard Razor parser and the Mvc Razor parser use the CodeTypeReferenceCollection type when parsing either the @inherits or @model keyword. Here's the code for @inherits during code generation:

protected internal virtual void VisitSpan(InheritsSpan span) {
  // Set the appropriate base type
  GeneratedClass.BaseTypes.Clear();
  GeneratedClass.BaseTypes.Add(span.BaseClass);

  if (DesignTimeMode) {
    WriteHelperVariable(span.Content, InheritsHelperName);
  }
}

GeneratedClass.BaseTypes 是一个 CodeTypeReferenceCollection - 而 span.BaseClass 是一个字符串.在 ILSpy 中,有问题的方法必须是私有方法 CodeTypeReference.Initialize(string typeName, CodeTypeReferenceOptions options).我现在没有足够的时间来弄清楚它为什么会中断 - 但我认为这是微软开发人员的工作:) 下面更新 - 无法抗拒.我现在知道哪里不对了

GeneratedClass.BaseTypes is a CodeTypeReferenceCollection - and span.BaseClass is a string. Following that through in ILSpy, the offending method must be the private method CodeTypeReference.Initialize(string typeName, CodeTypeReferenceOptions options). I've not enough time now to figure out why it breaks - but then that's a Microsoft developer's job I think :) Update below - couldn't resist. I now know where it's wrong

您不能在 Razor @inherits@model 语句中使用超过 4 个参数的泛型(至少在 C# 中 - 不了解 VB).Razor 解析器似乎错误地使用了 CodeTypeReference 类型.

You can't use generics with more than 4 parameters in either Razor @inherits or @model statements (at least in C# - don't know about VB). It appears that the Razor parser is incorrectly using the CodeTypeReference type.

CodeTypeReference 所做的一件事是通过调用方法 CodeTypeReference.RipOffAssemblyInformationFromTypeName(string typeName) 从传递的类型名称中剥离程序集名称信息.

One of the things that CodeTypeReference does is strip off assembly name information from a passed type name with a call to the method CodeTypeReference.RipOffAssemblyInformationFromTypeName(string typeName).

当然,如果你仔细想想 - Tuple 就像一个程序集限定的类型名称:类型名称 = 元组<t、Assembly = T、Version=T、Culture=T、PublicKeyToken=T>(如果您编写了一个非常糟糕的 C# 解析器!).</t

And of course, if you think about it - Tuple<T,T,T,T,T> is just like an assembly-qualified type name: With the type name = Tuple<T, Assembly = T, Version=T, Culture=T, PublicKeyToken=T (if you write a really BAD C# parser!).

果然 - 如果你传入 Tuple 作为类型名称 - 你实际上得到了一个 Tuple.

Sure enough - if you pass in Tuple<T,T,T,T,T,T> as the type name - you actually get a Tuple<T,T>.

深入研究代码,它准备接收一个语言中立的类型名(例如,处理 '[' 但没有处理 '<'),因此,实际上,MVC 团队不应该只处理 C# 类型名直接从我们的源头开始.

Looking deeper into the code, it's primed to receive a language-neutral typename (handles '[' but nothing for '<', for example) so, actually, the MVC team shouldn't just be handing the C# typename from our source straight through.

MVC 团队需要改变他们生成基本类型的方式 - 他们可以使用 public CodeTypeReference(string typeName, params CodeTypeReference[] typeArguments) 新引用的构造函数(而不是仅仅依靠 .Add(span.BaseClass) 创建它),并解析泛型参数本身,因为它们知道类型名称将是 C#/VB 风格 - 不是语言中立的 .Net 风格,带有括号等作为实际名称的一部分.

The MVC team needs to change how they generate the base type - They could use the public CodeTypeReference(string typeName, params CodeTypeReference[] typeArguments) constructor for a new reference (instead of just relying on the .Add(span.BaseClass) creating it), and parse the generic parameters themselves since they know that the type name will be C#/VB style - not language-neutral .Net style with brackets etc as part of the actual name.

这篇关于asp.net mvc 3 剃刀视图 ->元组问题的强类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1169

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1070

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:806

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:898

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:930

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:876

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:864

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:833

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1725

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1308