2016-06-11 152 views
-1

快速問題...我有一個項目,我加載基本模型到我的佈局頁面。在我的每個其他模型中,我能夠引用這個工作正常的BasePage模型。然而,如果我創建一個列出不同模型的視圖,我會收到編譯錯誤...ASP.NET MVC 5模型在查看問題

爲了更清楚地解釋,我調用了一個視圖,該視圖詳細列出了用戶的類註冊列表。這個型號是IEnumerable<Enrollment>。細節視圖對每個註冊都適用,但是當我嘗試獲取完整列表時,出現編譯錯誤,告訴我該字典需要模型類型BasePage

任何清晰度都會很棒!

對不起,如果這是令人困惑,我很新的.NET ......主要是PHP的背景。

BasePage的型號 -

public abstract class BasePage 
{ 

    public BasePage() 
    { 
     public string prop { get; set; } 
    } 
} 

招生模式 -

public class Enrollment : BasePage 
{ 
    #region modelsettings 

    private static PageDisplayItem PAGE_DISPLAYITEM = new PageDisplayItem() 
    { 
     PageNavigationType = PageNavigationType.FullNavigation, 
     HasSideNavigation = true, 
     HasPageControl = true, 
     HasTableHeader = true, 
     HasPageHeader = true, 
     IsBackgroundDark = false, 
     IsContentTransparent = false 
    }; 

    #endregion 

    public Enrollment() 
    { 
     this.SetPageDisplay(this, PAGE_DISPLAYITEM); 
    } 

    public Enrollment(bool isInitialize, string entityGUID = "") 
    { 
     // common props 
     var session = Helper.GetSession(); 
     var planRepo = new PlanRepository(); 
     var cmnRepo = new CommonRepository(); 


     // set guid 
     if (!string.IsNullOrEmpty(entityGUID)) 
     { 
      var entityRepo = new EntityRepository(); 
      this.EntityItem = entityRepo.ReturnEntityOrUserItem(this.PageLanguageType, entityGUID); 

      if (this.EntityItem.EntityID == 0) 
      { 
       this.EntityItem = new EntityItem() 
       { 
        EntityGUID = session.SessionUserItem.UserGUID, 
        EntityName = session.SessionUserItem.UserFullName, 
        EntityID = session.SessionUserItem.UserID, 
        EntityType = EntityType.User 
       }; 
      } 
     } 
     else 
     { 
      this.EntityItem = new EntityItem() 
      { 
       EntityGUID = session.SessionUserItem.UserGUID, 
       EntityName = session.SessionUserItem.UserFullName, 
       EntityID = session.SessionUserItem.UserID, 
       EntityType = EntityType.User 
      }; 
     } 



     // init 
     if (isInitialize) 
     { 
      // page 
      this.SetPageDisplay(this, PAGE_DISPLAYITEM); 


     } 
    } 

    #region modelprops 

    // BasePage properties 
    public EntityItem EntityItem { get; set; } 
    public bool IsCurrentPeriod { get; set; } 
    public PeriodItem DisplayPeriod { get; set; } 
    public List<PeriodItem> PeriodList { get; set; } 

    // Enrollment properties 
    public string Id { get; set; } 


    #endregion 
} 

這裏是_Layout.cshtml頁......我切出的膽量爲簡潔起見:

@model BasePage 

@{ 
    var m = @Model; 
} 
<!DOCTYPE html> 
<html> 
<head> 

    <title>@m.PageTabTitle @m.PageTabTitleExtension</title> 

以下是我創建的用戶模型,用於顯示可正常工作的用戶模型的詳細信息BasePage的模式從用戶模式:

public class User : BasePage 
{ 

    #region modelsettings 

    private static PageDisplayItem PAGE_DISPLAYITEM = new PageDisplayItem() 
    { 
     PageNavigationType = PageNavigationType.FullNavigation, 
     HasSideNavigation = true, 
     HasPageControl = true, 
     HasTableHeader = true, 
     HasPageHeader = true, 
     IsBackgroundDark = false, 
     IsContentTransparent = false 
    }; 

    #endregion 

    public User() 
    { 
     this.SetPageDisplay(this, PAGE_DISPLAYITEM); 
    } 

    public User(bool isInitialize, string entityGUID = "") 
    { 
     // common props 
     var session = Helper.GetSession(); 
     var planRepo = new PlanRepository(); 
     var cmnRepo = new CommonRepository(); 


     // set guid 
     if (!string.IsNullOrEmpty(entityGUID)) 
     { 
      var entityRepo = new EntityRepository(); 
      this.EntityItem = entityRepo.ReturnEntityOrUserItem(this.PageLanguageType, entityGUID); 

      if (this.EntityItem.EntityID == 0) 
      { 
       this.EntityItem = new EntityItem() 
       { 
        EntityGUID = session.SessionUserItem.UserGUID, 
        EntityName = session.SessionUserItem.UserFullName, 
        EntityID = session.SessionUserItem.UserID, 
        EntityType = EntityType.User 
       }; 
      } 
     } 
     else 
     { 
      this.EntityItem = new EntityItem() 
      { 
       EntityGUID = session.SessionUserItem.UserGUID, 
       EntityName = session.SessionUserItem.UserFullName, 
       EntityID = session.SessionUserItem.UserID, 
       EntityType = EntityType.User 
      }; 
     } 



     // init 
     if (isInitialize) 
     { 
      // page 
      this.SetPageDisplay(this, PAGE_DISPLAYITEM); 


     } 
    } 


#region modelprops 

    // BasePage props 
    public EntityItem EntityItem { get; set; } 
    public bool IsCurrentPeriod { get; set; } 
    public PeriodItem DisplayPeriod { get; set; } 
    public List<PeriodItem> PeriodList { get; set; } 

    // User props 
    public string Id { get; set; } 

#endregion 


} 

這被用於用戶的詳細信息,.cshtml網頁,正常工作與_Layout.cshtml:

@model FD.Models.User 

@{ 
    ViewBag.Title = "User Profile"; 
    Layout = "~/Views/Shared/_BaseLayout.cshtml"; 
    var m = Model; 
} 

最後,這裏是爲.cshtml 。

@model IEnumerable<FD.Models.Enrollment> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<table class="table"> 
// Again, I cut out the rest for the sake of brevity. 

這裏是:表明他們目前正在就讀於用戶的類招生名單出於某種原因,當我用了IEnumerable類型是在編譯過程中爆炸我在瀏覽器中得到的編譯錯誤:

'/'應用程序中的服務器錯誤。

傳遞到詞典中的模型產品 類型的「System.Collections.Generic.List`1 [FD.Models.Enrollment]」,但這 詞典需要類型「FD.WWW.Models的模型項.BasePage」。

描述:在執行 當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:System.InvalidOperationException:傳遞到詞典中的模型項 是 類型的「System.Collections.Generic.List`1 [FD.Models.Enrollment]」,但這 字典需要一個模型項類型'FD.WWW.Models.BasePage'。

+0

您能提供任何代碼示例,以進一步解釋您的問題? –

+2

你將不得不在代碼中用[mcve] – Nkosi

回答

1

假設你_layout頁面也正在被其他頁面引用那麼問題是,你已經在_layout定義模型BasePage類型,然後在你的註冊頁面,您將其更改爲IEnumerable<FD.Models.Enrollment>這將導致一個問題因爲它預期從_Layout中聲明的BasePage派生的模型,當您將模型從Controller傳遞到View時將優先考慮。雖然FD.Models.Enrollment來自BasePage,IEnumerable<FD.Models.Enrollment>不。

用戶頁面仍然有效的原因是因爲其型號FD.Models.User來自FD.WWW.Models.BasePage

您可以

  1. 從所使用的註冊頁面使用一個_Layout.cshtml
  2. 或更改佈局刪除BasePage模式,不具備BasePage,因爲它的模式。

    @{Layout = "~/Views/Shared/_SomeOtherLayout.cshtml";} 
    @model IEnumerable<FD.Models.Enrollment> 
    
    <p> 
        @Html.ActionLink("Create New", "Create") 
    </p> 
    <table class="table">