Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add View Model class dropdown not showing my class

Tags:

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.

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:

  1. In Visual Studio 2010 (sp1), create a new C# asp.net MVC 4.0 (beta) Internet Application.
  2. Inside the Models folder, create a new simple class called “GazouViewModel.cs”
  3. Build the application, open the HomeController, right-click the Index() ActionResult and select Add View…
  4. In the Add View dialog, check the “strongly-typed view” checkbox and in the Model class: dropdown, you’ll notice your “GazouViewModel”

  1. In Solution Explorer, right-click your project and select “Manage NuGet Packages”
  2. Search, find and install the “Bootstrapper.Autofac” package.
  3. Rebuild the application and verify if your “GazouViewModel” still displays inside the Add View dropdown’s dialog.
  4. The “GazouViewModel” should still be displaying in the dropdown.

  1. In Solution Explorer, right-click your project and select “Manage NuGet Packages”
  2. Search, find and install the “Autofac ASP.NET MVC4 (Beta) Integration” package.
  3. Rebuild the application and verify if your “GazouViewModel” still displays inside the Add View dropdown’s dialog.
  4. The “GazouViewModel” should still be displaying in the dropdown.

  1. In Solution Explorer, right-click your project and ADD a new class (Class1.cs) anywhere it doesn’t matter.
  2. Make that Class1 implement IAutofacRegistration and implement the interface.
  3. Rebuild the application and verify if your “GazouViewModel” still displays inside the Add View dropdown’s dialog.
  4. The “GazouViewModel” is no longer showing inside the Model class dropdown.

It appears that as soon as I create a class that implements the IAutofacRegistration interface and build the application, I’m no longer capable of viewing my ViewModels in that dropdown.

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.

Sincerely Vince

like image 488
Vlince Avatar asked Mar 12 '12 13:03

Vlince


People also ask

How do I add a model class to a view?

Go to solution explorer => Views Folder => Right-click on “Model” Folder >> go to “Add” >> Click on [Class] as follow. Provide the required name like “Product. cs” then click on “Add” button as follow.

How do I update view model?

When you click the submit button, the HttpPost action method above will be executed and the value of model. FavoriteColor will be what you select in the dropdownlist. After that you can use that value to update your database. and you can get the person id in model.

How do I access model value in view?

In Solution Explorer, right-click the Controllers folder and then click Add, then Controller. In the Add Scaffold dialog box, click MVC 5 Controller with views, using Entity Framework, and then click Add. Select Movie (MvcMovie. Models) for the Model class.


1 Answers

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()

<#@ 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"; } 
like image 62
user1583948 Avatar answered Oct 11 '22 13:10

user1583948