1

当我尝试使用脚手架模板从控制器方法添加视图时出现错误。我选择哪个模板并不重要,我得到一个错误。我正在使用 VS2012 更新 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 时出现异常。转换将不会运行。引发了以下异常: System.NullReferenceException:对象引用未设置为对象的实例。在 System.Reflection.RuntimeAssembiy._nLoad(AssembtyName 文件名,字符串 codeBase,证据 assemblySecurity,RuntimeAssembly locationHint,StackCrawlMark 和 stackMark,IntPtr pPrivHostBinder,布尔 throwOnFileNotFound,布尔自省,布尔 suppressSecurityChecks)在 System.Reflection.RuntimeAssembly.nLoad(AssemblyName 文件名,字符串 codeBase , 证据 assemblySecurity, RuntimeAssembly locationHint。StackCrawlMark& stackMark,IntPtr pPrivHostBinder,布尔 throwOnFileNotFound,布尔用于自省。Boolean suppressSecurityChecks) 在 System,Reflection.RuntimeAssemblyjnternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark & stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean for Introspection, Boolean suppressSecurityChecks) 在 System.Reflection.RuntimeAssemblyinternalLoad(String assemblyMarkString. E IntPtr pPrivf-lostBinder, Boolean for introspection) 在 System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString. Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean for introspection) 在 System.Reflection.Assembly.t.oad(String assemblyString) 在 Microsoft.VisualStudio.Web。 MV。

更新:我不能在这里发布图片,所以链接到下面的属性。更改复制本地不会改变行为。

DataAnnotations 属性

4

2 回答 2

7

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

于 2013-05-19T19:52:46.620 回答
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);
        }
    }
于 2013-05-16T00:32:39.313 回答