MVC 4从控制器添加脚手架视图会引发错误

时间:2013-05-16 00:02:58

标签: asp.net-mvc-4

当我尝试使用脚手架模板从控制器方法添加视图时出现错误。我选择哪个模板并不重要,我收到错误。我正在使用VS2012 Update 2.

控制器方法和模型是:

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

我不知道出了什么问题。是的,我引用了错误消息中提到的程序集。程序集是System.ComponentModel.DataAnnotations,System.core,System.Data.Entity和System.Data.Linq。我只放置了四条消息中的一条 - 太长了。

  

C:\ Program Files(x86)\ Microsoft Visual Studio 11.O \ Common7JDE \ IteniTemplates \ CSharp \ Web \ MVC 4 \ CodeTemplates \ AddView \ CSHTML \ Details.tt(-1,-1):错误:尝试解析程序集引用System.ComponentModel.DataAnnotations时,host抛出异常。转换不会运行。抛出以下异常:System.NullReferenceException:未将对象引用设置为对象的实例。 System.Reflection.RuntimeAssembly.nLoad的System.Reflection.RuntimeAssembiy._nLoad(AssembtyName fileName,String codeBase,Evidence assemblySecurity,RuntimeAssembly locationHint,StackCrawlMark& stackMark,IntPtr pPrivHostBinder,Boolean throwOnFileNotFound,Boolean forintrospection,Boolean suppressSecurityChecks)(AssemblyName fileName,String) 。的代码库,证据assemblySecurity,RuntimeAssembly locationHint StackCrawlMark&安培; stackMark,IntPtr的pPrivHostBinder,布尔throwOnFileNotFound,布尔forintrospection布尔suppressSecurityChecks)在系统,Reflection.RuntimeAssemblyjnternalLoadAssemblyName(的AssemblyName assemblyRef,证据assemblySecurity,RuntimeAssembly reqAssembly,StackCrawlMark&安培; stackMark,IntPtr的pPrivHostBinder,布尔throwOnFileNotFound, System.Reflection.RuntimeAssemblyinternalLoad(String assemblyString。证据assemblySecurity,StackCrawlMark& stackMark,IntPtr pPrivf-lostBi)中的布尔forlntrospection,布尔值suppressSecurityChecks) nder,Boolean forintrospection)在System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString。证据集合安全,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主机,TemplateProcessingSession会话)

更新:我无法在此处发布图片,因此请链接到以下属性。更改“复制本地”不会改变行为。

DataAnnotations properties

2 个答案:

答案 0 :(得分:7)

我遇到了完全相同的问题,它与Microsoft Code Digger扩展0.95有关。禁用扩展允许脚手架进程成功运行而没有任何错误。

答案 1 :(得分:0)

我必须运行它来实际查看正在发生的事情,而且我无法判断您是否列出了程序集或错误代码。但这是我认为可能是错的。欢迎你试试看。

    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);
        }
    }