asp.net-mvc 添加视图模型类下拉列表未显示我的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9668140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add View Model class dropdown not showing my class
提问by Vlince
I've created a new C# asp.net mvc 4.0 (beta) Internet Application and I'm having a somewhat odd behavior in the Add View dialog in Visual Studio 2010 (sp1). When I decide to create a strongly-typed View, inside the Add View dialog, the Model class dropdown does not show my ViewModel class.
我创建了一个新的 C# asp.net mvc 4.0 (beta) Internet 应用程序,并且在 Visual Studio 2010 (sp1) 的“添加视图”对话框中出现了一些奇怪的行为。当我决定创建一个强类型视图时,在添加视图对话框中,模型类下拉列表不会显示我的 ViewModel 类。
For the record, yes I did compile the application before triggering the Add View dialog.
作为记录,是的,我确实在触发“添加视图”对话框之前编译了应用程序。
I believe I have pinpointed the issue but do not know how to fix it. Below are the repro-steps if anyone cares to reproduce the behavior:
我相信我已经查明了问题,但不知道如何解决。如果有人想重现该行为,以下是重现步骤:
- In Visual Studio 2010 (sp1), create a new C# asp.net MVC 4.0 (beta) Internet Application.
- Inside the Models folder, create a new simple class called “GazouViewModel.cs”
- Build the application, open the HomeController, right-click the Index() ActionResult and select Add View…
- In the Add View dialog, check the “strongly-typed view” checkbox and in the Model class: dropdown, you'll notice your “GazouViewModel”
- 在 Visual Studio 2010 (sp1) 中,创建一个新的 C# asp.net MVC 4.0 (beta) Internet 应用程序。
- 在 Models 文件夹中,创建一个名为“GazouViewModel.cs”的新简单类
- 构建应用程序,打开 HomeController,右键单击 Index() ActionResult 并选择 Add View…
- 在“添加视图”对话框中,选中“强类型视图”复选框,然后在“模型类:”下拉列表中,您会注意到“GazouViewModel”
- In Solution Explorer, right-click your project and select “Manage NuGet Packages”
- Search, find and install the “Bootstrapper.Autofac” package.
- Rebuild the application and verify if your “GazouViewModel” still displays inside the Add View dropdown's dialog.
- The “GazouViewModel” should still be displaying in the dropdown.
- 在解决方案资源管理器中,右键单击您的项目并选择“管理 NuGet 包”
- 搜索、查找并安装“Bootstrapper.Autofac”包。
- 重新构建应用程序并验证您的“GazouViewModel”是否仍显示在“添加视图”下拉对话框中。
- “GazouViewModel”应该仍然显示在下拉列表中。
- In Solution Explorer, right-click your project and select “Manage NuGet Packages”
- Search, find and install the “Autofac ASP.NET MVC4 (Beta) Integration” package.
- Rebuild the application and verify if your “GazouViewModel” still displays inside the Add View dropdown's dialog.
- The “GazouViewModel” should still be displaying in the dropdown.
- 在解决方案资源管理器中,右键单击您的项目并选择“管理 NuGet 包”
- 搜索、查找并安装“Autofac ASP.NET MVC4(Beta)集成”包。
- 重新构建应用程序并验证您的“GazouViewModel”是否仍显示在“添加视图”下拉对话框中。
- “GazouViewModel”应该仍然显示在下拉列表中。
- In Solution Explorer, right-click your project and ADD a new class (Class1.cs) anywhere it doesn't matter.
- Make that Class1 implement IAutofacRegistration and implement the interface.
- Rebuild the application and verify if your “GazouViewModel” still displays inside the Add View dropdown's dialog.
- The “GazouViewModel” is no longer showing inside the Model class dropdown.
- 在解决方案资源管理器中,右键单击您的项目并在任何无关紧要的地方添加一个新类 (Class1.cs)。
- 使 Class1 实现 IAutofacRegistration 并实现接口。
- 重新构建应用程序并验证您的“GazouViewModel”是否仍显示在“添加视图”下拉对话框中。
- “GazouViewModel”不再显示在模型类下拉列表中。
It appears that as soon as I create a class that implements the IAutofacRegistrationinterface and build the application, I'm no longer capable of viewing my ViewModels in that dropdown.
似乎一旦我创建了一个实现IAutofacRegistration接口并构建应用程序的类,我就无法再在该下拉列表中查看我的 ViewModel。
Can anyone confirm if they are getting the same results as me? And more importantly, how can one fix this?
谁能确认他们是否得到了与我相同的结果?更重要的是,如何解决这个问题?
IMPORTANT: I have tried the exact same steps with an mvc 3.0 application and the exact same behavior occurs…so this is not relevant to the BETA version.
重要提示:我已经对 mvc 3.0 应用程序尝试了完全相同的步骤,并且发生了完全相同的行为……所以这与 BETA 版本无关。
Sincerely Vince
真诚的文斯
采纳答案by Vlince
UPDATE:As of this writing, I can no longer reproduce the error. Perhaps the NuGet packages I use have been updated to a newer version and incidentally fixed the error.
更新:在撰写本文时,我无法再重现该错误。也许我使用的 NuGet 包已更新到较新的版本并顺便修复了错误。
I will mark this post as closed.
我会将这篇文章标记为已关闭。
Thanks for everyone's input on this!
感谢大家对此的投入!
Vince
文斯
回答by user1583948
If you get error message: "Running transformation: The input file appears to be using a schema version not supported by this template. This may lead to compile errors. Please use 'Add New Generated Item' to add an updated template".
如果您收到错误消息:“正在运行转换:输入文件似乎正在使用此模板不支持的架构版本。这可能会导致编译错误。请使用‘添加新生成的项目’来添加更新的模板”。
You are used EF 5.x. You have to add metadata for EF 5.x to your T4 template. In file header call DefineMetadata()
您使用的是 EF 5.x。您必须将 EF 5.x 的元数据添加到您的 T4 模板。在文件头中调用 DefineMetadata()
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ include file="EF.Utility.CS.ttinclude"#><#@ output extension=".cs"#><#
DefineMetadata();
at the bottom of your template create method
在模板创建方法的底部
private void DefineMetadata()
{
TemplateMetadata[MetadataConstants.TT_TEMPLATE_NAME] = "CSharpDbContext.Types";
TemplateMetadata[MetadataConstants.TT_TEMPLATE_VERSION] = "5.0";
TemplateMetadata[MetadataConstants.TT_MINIMUM_ENTITY_FRAMEWORK_VERSION] = "5.0";
}
回答by Simon Sessing?
I solved this issue by beeing mad in like 2 hours and then deleting the Assembly.cs from the Properties-folder in the solution. After I made a rebuild of the solution, the "Add-view" model-dropdown suddently could find all my ViewModels, strange enough.
我通过在大约 2 小时内发疯然后从解决方案的 Properties 文件夹中删除 Assembly.cs 解决了这个问题。在我重新构建解决方案后,“添加视图”模型下拉菜单突然可以找到我所有的视图模型,这很奇怪。
I guess the assembly version defined in Assembly.cs somehow got mixed up with the actual version used in the project. Anyhow, it works now for my case.
我猜在 Assembly.cs 中定义的程序集版本以某种方式与项目中使用的实际版本混淆了。无论如何,它现在适用于我的情况。
回答by Sergio Flores
What worked for me was to simply build the solution after creating my model. Then I was able to find my model in the dropdownlist.
对我有用的是在创建模型后简单地构建解决方案。然后我能够在下拉列表中找到我的模型。
回答by Ivan Karajas
I've noticed this issue as well (using MVC 4 Beta on Visual Studio 2010). Re-starting Visual Studio solved the problem for me.
我也注意到了这个问题(在 Visual Studio 2010 上使用 MVC 4 Beta)。重新启动 Visual Studio 为我解决了这个问题。
回答by Gerard Wilkinson
Just another solution to add to the pile as none of the other ones worked for me. This is another example of Visual Studio being terrible at handling long file paths and namespaces.
只是另一种添加到堆中的解决方案,因为其他解决方案都不适合我。这是 Visual Studio 在处理长文件路径和命名空间方面很糟糕的另一个例子。
In my case the namespace was too long to display in the "Model class" dropdown. Shortening the namespace (a ridiculous pain in of itself) made the drop down work again!
在我的情况下,命名空间太长而无法在“模型类”下拉列表中显示。缩短命名空间(本身就是一种荒谬的痛苦)使下拉菜单再次起作用!
回答by Kiran Waghmare
Just Clean and build your solution. It worked for me.
只需清理并构建您的解决方案。它对我有用。
回答by Craig B
I realize this is a bit out of date. I had a similar situation where my classes wouldn't load into the View Template wizard. I'm using VS 2012 with MVC3. Everyone else in the team was OK, just luckily old me had issues. After much research and trying lots of things the only thing that has consistently fixed it is running VS in safemode (from the command prompt: "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe" /safemode). One of my extensions must be causing the issue and by running in Safe Mode those aren't loaded.
我意识到这有点过时了。我遇到过类似的情况,我的类无法加载到视图模板向导中。我在 MVC3 中使用 VS 2012。团队中的其他人都很好,只是幸运的是老我有问题。经过大量研究和尝试很多事情后,唯一一直修复它的是在安全模式下运行 VS(从命令提示符:“C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe”) /安全模式)。我的扩展程序之一一定是导致问题的原因,并且在安全模式下运行未加载这些扩展程序。
回答by Jayce
Using VS 2010 and MVC 3.0, at one point the Add View Model class dropdown was not showing any classes for me. Ivan's suggestion of restarting VS appeared to work for me.
使用 VS 2010 和 MVC 3.0,在某一时刻,添加视图模型类下拉列表没有为我显示任何类。伊万关于重新启动 VS 的建议似乎对我有用。
I tried deleting AssemblyInfo.cs, did a Clean, and a Rebuild, but those didn't resolve the issue. When I restarted VS, the model classes were back in the dropdown.
我尝试删除 AssemblyInfo.cs、清理和重建,但这些都没有解决问题。当我重新启动 VS 时,模型类又回到了下拉列表中。

