c# - 脚手架和显示/编辑器模板

标签 c# asp.net-mvc asp.net-mvc-3 ef-code-first asp.net-mvc-scaffolding

假设我有以下模型:

public class Item
{
    public int Id{ get; set; }
    public ItemDescription ItemDescription{ get; set; }
}
public class ItemDescription
{
    public int Id{ get; set; }
    public int Revision{ get; set; }
    public string Test{ get; set; }
}

我在包管理器中运行以下命令:

Scaffold Controller Item

生成一些 View 和东西...但脚手架似乎忽略了任何复杂/非标量数据类型,因此生成的 View 几乎没有用。

我想知道是否可以指示脚手架对事物更加智能。这是我希望发生的事情:

  • 脚手架在共享文件夹中创建编辑器/显示模板
  • 使用 EditorFor 来利用这些模板

实现这一目标的所有代码似乎都是由脚手架生成的,但其结构让我感到惊讶,_CreateOrEdit.cshtml 在与 View 关联的文件夹中生成了"template" .对我来说,这表明脚手架生成的代码不适合以更递归的方式为模型生成 View 。

是我的期望偏离了,还是我遗漏了什么?

最佳答案

描述

您可以在中找到 Visual Studio 用于生成代码的 T4 模板

YourPathToVisualStudio2010\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates

更改解决一般问题所需的模板。您可以做的另一件事是创建一个模板并将它们添加到您的特定项目/解决方案中。

看看很棒的 Scott Hanselman - T4 (Text Template Transformation Toolkit) Code Generation - Best Kept Visual Studio Secret发布。

我真的鼓励您阅读我的“更多信息”部分中的链接。

祝你有美好的一天!

更新

几天以来,整个(不仅是 mvc)microsoft webstack(爱,thx scott hanselman for this term ;))在 codeplex 上开源 ASP.NET Webstack .检查一下!

MVC4 没有 RTM 版本,但也许你有兴趣。

Recipes for Code Generation in Visual Studio (MVC4)

The new Recipes feature enables Visual Studio to generate solution-specific code based on packages that you can install using NuGet. The Recipes framework makes it easy for developers to write code-generation plugins, which you can also use to replace the built-in code generators for Add Area, Add Controller, and Add View. Because recipes are deployed as NuGet packages, they can easily be checked into source control and shared with all developers on the project automatically. They are also available on a per-solution basis.

请随时提出其他问题。请使用我的回答的评论功能。

更多信息

关于c# - 脚手架和显示/编辑器模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8357211/

相关文章:

asp.net-mvc - ASP.Net MVC 3 网格

c# - .Net Core C# 将方法/类作为子进程执行

c# - 为什么内部 TransactionScope 的 IsolationLevel 不能不同,而 SQL 事务可以不同?

jquery - 使用 jquery 的 ASP.NET MVC Ajax 可扩展表单

c# - 为什么 ASP.NET 的“记住我”功能如此健忘?

c# - 如何在 C# 中使用复杂类型的 REST 服务?

mysql - 在 ASP.NET MVC 3 中使用 MySQL 数据库的 Linq,将 DateTime 存储到变量中

asp.net-mvc - MVC 下领域驱动设计的替代方案是什么

c# - 何时以及如何使用基于属性的编程

c# - 在 C# StreamGeometry Xaml 标记中创建非描边区域