user1268114
user1268114

Reputation: 15

MVC 4 Add scaffolding view from controller throws error

I get an error when I attempt to add a view from a controller method using a scaffolding template. It doesn't really matter which template I choose, I get an error. I am using VS2012 Update 2.

The controller method and model are:

  public ActionResult EditInventoryItem(int id)
  {
     InventoryRepository repo = new InventoryRepository();
     Inventory inventoryItem = repo.GetSingle(id);
     return View(inventoryItem);
  }

    public class Inventory
   {
      public int Id { get; set; }
      public string Number { get; set; }
      public string Description { get; set; }
      public string Category { get; set; }
      public string Group { get; set; }
      public string Manufacturer { get; set; }
      public string ManufacturerModelNumber { get; set; }
   }

I do not have a clue what is wrong. Yes, I have references to the assemblies mentioned in the error messages. The assemblies are System.ComponentModel.DataAnnotations, System.core, System.Data.Entity and System.Data.Linq. I am placing only one of the four messages - too long.

C:\Program Files (x86)\Microsoft Visual Studio 11.O\Common7JDE\IteniTemplates\CSharp\Web\MVC 4\CodeTemplates\AddView\CSHTML\Details.tt(-1,-1): error: The host threw an exception while trying to resolve the assembly reference System.ComponentModel.DataAnnotations. The transformation will not be run. The following Exception was thrown: System.NullReferenceException: Object reference not set to an instance of an object. at System.Reflection.RuntimeAssembiy._nLoad(AssembtyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forintrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint. StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forintrospection. Boolean suppressSecurityChecks) at System,Reflection.RuntimeAssemblyjnternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forlntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssemblyinternalLoad(String assemblyString. Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivf-lostBinder, Boolean forintrospection) at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString. Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forintrospection) at System.Reflection.Assembly.t.oad(String assemblyString) at Microsoft.VisualStudio.Web.Mvc.Userlnterface.MvcTextTemplateHost.ResolveAssemb)yReference(String assemblyReference) at Microsoft.VisualStudio.TextTemplating.Engine.ResolveAssemblyReferences(ITextTemplatingEngineHost host, TemplateProcessingSession session)

Update: I cannot post images here, so link to properties below. Changing Copy Local did not alter behavior.

DataAnnotations properties

Upvotes: 1

Views: 835

Answers (2)

sophokless
sophokless

Reputation: 108

I had exactly the same problem and it was related to the Microsoft Code Digger extension 0.95. Disabling the extension allowed the scaffolding process to work sucessfully without any errors.

Upvotes: 7

LOTUSMS
LOTUSMS

Reputation: 10270

I would have to run it to actually see what is going on, plus I couldn't tell if you were listing your assemblies or the error code. But Here is what I think may be wrong. You're welcome to try it and see.

    public class InveentoryController : Controller
    {
       InventoryRepository repo = new InventoryRepository(); 

        public ActionResult EditInventoryItem(int id = 0)   
        {         
           Inventory inventoryItem = repo.inventoryItem.Find(id);
               if (inventoryItem == null)
           {
               return HttpNotFound();
           }
           return View(inventoryItem);
        }
    }

Upvotes: 0

Related Questions