5

Mam częściowy widok, który używa innego modelu niż widok, w którym jest renderowany. Ciągle otrzymuję komunikat o błędzie.Inny model w widoku częściowym

Przekazany do słownika poz model jest typu „JHelpWebTest2.Models.CombinedModels”, ale ten słownik wymaga elementu modelu typu „JHelpWebTest2.Models.PagedStudentModel”.

Nie jestem pewien, jak to naprawić, to niektóre z mojego kodu. Mój pogląd Index:

@using System.Activities.Expressions 
@using JHelpWebTest2.Models 
@model JHelpWebTest2.Models.CombinedModels 

@using (Html.BeginForm("_Grid", "Sort")) 
{ 
    @Html.Partial("~/Views/Sort/_Grid.cshtml")     
} 

Oto moja _Grid częściowy widok

@model JHelpWebTest2.Models.PagedStudentModel 
@using JHelpWebTest2.Models; 
<div id="grid"> 

     @{ 
      var grid1 = new WebGrid(rowsPerPage: Model.PageSize, defaultSort: "YR_CDE", ajaxUpdateContainerId: "grid"); 
      grid1.Bind(Model.Studentcrshist, autoSortAndPage: false, rowCount: Model.TotalRows); 
      grid1.Pager(WebGridPagerModes.All);     
     } 
     @grid1.GetHtml(
      tableStyle: "webGrid", 
      headerStyle: "header", 
      alternatingRowStyle: "alt", 
      mode: WebGridPagerModes.All, 
      firstText: "<< First", 
       previousText: "< Prev", 
       nextText: "Next >", 
       lastText: "Last >>", 
      columns: grid1.Columns(
       grid1.Column("YR_CDE", "YR_CDE"), 
       grid1.Column("TRM_CDE", "TRM_CDE"), 
       grid1.Column("SUBTERM_CDE", "SUBTERM_CDE"), 
       grid1.Column("CRS_CDE", "CRS_CDE"), 
       grid1.Column("CRS_DIV", "CRS_DIV"), 
       grid1.Column("CREDIT_HRS", "CREDIT_HRS"), 
       grid1.Column("CRS_TITLE", "CRS_TITLE"), 
       grid1.Column("ADD_FLAG", "ADD_FLAG"), 
       grid1.Column("ADD_DTE", "ADD_DTE", format: (model => model.ADD_DTE != null ? model.ADD_DTE.ToString("MM/dd/yyyy") : "")), 
       grid1.Column("DROP_FLAG", "DROP_FLAG"), 
       grid1.Column("DROP_DTE", "DROP_DTE", format: (model => model.DROP_DTE != null ? model.DROP_DTE.ToString("MM/dd/yyyy") : "")) 
       )) 
</div> 

To CombinedModel:

namespace JHelpWebTest2.Models 
{ 
    public class CombinedModels 
    { 
     public NAME_MASTER NAME_MASTER { get; set; } 
     public AddressModel DefaultAddressModel { get; set; } 
     public IEnumerable<AddressModel> AllAddressModels { get; set; } 
     public STUDENT_MASTER STUDENT_MASTER { get; set; } 
     public STUDENT_DIV_MAST STUDENT_DIV_MAST { get; set; } 
     public BIOGRAPH_MASTER BiographMaster { get; set; } 
     public TW_WEB_SECURITY Security { get; set; } 
     public ADVISOR_STUD_TABLE Advisor { get; set; } 
     public CANDIDACY Candidacy { get; set; } 
     public IEnumerable<STUDENT_CRS_HIST> StudentCrsHist { get; set; } 
     public STUDENT_CRS_HIST StudentCrsHist1 { get; set; } 
     public IEnumerable<TERM_DEF> TermDef { get; set; } 
     public IEnumerable<SUBTERM_DEF> SubtermDef { get; set; } 
     public IEnumerable<YEAR_DEF> YearDef { get; set; } 
     public NAME_AND_ADDRESS NameAndAddress { get; set; } 
     public PagedStudentModel PagedStudentModel { get; set; } 
    } 
} 

To jest mój model PagedStudentModel

namespace JHelpWebTest2.Models 
{ 
    public static class SortModel 
    { 
     public static IOrderedEnumerable<TSource> OrderByWithDirection<TSource, TKey> 
      (this IEnumerable<TSource> source, 
      Func<TSource, TKey> keySelector, 
      bool descending) 
     { 
      return descending ? source.OrderByDescending(keySelector) 
           : source.OrderBy(keySelector); 
     } 

     public static IOrderedQueryable<TSource> OrderByWithDirection<TSource, TKey> 
      (this IQueryable<TSource> source, 
      Expression<Func<TSource, TKey>> keySelector, 
      bool descending) 
     { 
      return descending ? source.OrderByDescending(keySelector) 
           : source.OrderBy(keySelector); 
     } 
    } 

    public class ModelServices : IDisposable 
    { 
     private readonly TmsEPrdEntities entities = new TmsEPrdEntities(); 

     public IEnumerable<STUDENT_CRS_HIST> GetStudentHistory(int pageNumber, int pageSize, string sort, bool Dir) 
     { 
      if (pageNumber < 1) 
       pageNumber = 1; 

      if (sort == "YR_CDE") 
       return entities.STUDENT_CRS_HIST.OrderByWithDirection(x => x.YR_CDE, Dir) 
        .Skip((pageNumber - 1) * pageSize) 
        .Take(pageSize) 
        .ToList(); 

      else if (sort == "TRM_CDE") 
       return entities.STUDENT_CRS_HIST.OrderByWithDirection(x => x.TRM_CDE, Dir) 
        .Skip((pageNumber - 1) * pageSize) 
        .Take(pageSize) 
        .ToList(); 

      else if (sort == "SUBTERM_CDE") 
       return entities.STUDENT_CRS_HIST.OrderByWithDirection(x => x.SUBTERM_CDE, Dir) 
        .Skip((pageNumber - 1) * pageSize) 
        .Take(pageSize) 
        .ToList(); 

      else if (sort == "CRS_CDE") 
       return entities.STUDENT_CRS_HIST.OrderByWithDirection(x => x.CRS_CDE, Dir) 
        .Skip((pageNumber - 1) * pageSize) 
        .Take(pageSize) 
        .ToList(); 

      else if (sort == "CRS_DIV") 
       return entities.STUDENT_CRS_HIST.OrderByWithDirection(x => x.CRS_DIV, Dir) 
        .Skip((pageNumber - 1) * pageSize) 
        .Take(pageSize) 
        .ToList(); 

      else if (sort == "CREDIT_HRS") 
       return entities.STUDENT_CRS_HIST.OrderByWithDirection(x => x.CREDIT_HRS, Dir) 
        .Skip((pageNumber - 1) * pageSize) 
        .Take(pageSize) 
        .ToList(); 

      else if (sort == "CRS_TITLE") 
       return entities.STUDENT_CRS_HIST.OrderByWithDirection(x => x.CRS_TITLE, Dir) 
        .Skip((pageNumber - 1) * pageSize) 
        .Take(pageSize) 
        .ToList(); 

      else if (sort == "ADD_FLAG") 
       return entities.STUDENT_CRS_HIST.OrderByWithDirection(x => x.ADD_FLAG, Dir) 
        .Skip((pageNumber - 1) * pageSize) 
        .Take(pageSize) 
        .ToList(); 

      else if (sort == "ADD_DTE") 
       return entities.STUDENT_CRS_HIST.OrderByWithDirection(x => x.ADD_DTE, Dir) 
        .Skip((pageNumber - 1) * pageSize) 
        .Take(pageSize) 
        .ToList(); 

      else if (sort == "DROP_FLAG") 
       return entities.STUDENT_CRS_HIST.OrderByWithDirection(x => x.DROP_FLAG, Dir) 
        .Skip((pageNumber - 1) * pageSize) 
        .Take(pageSize) 
        .ToList(); 

      else 
       return entities.STUDENT_CRS_HIST.OrderByWithDirection(x => x.ID_NUM, Dir) 
        .Skip((pageNumber - 1) * pageSize) 
        .Take(pageSize) 
        .ToList(); 
     } 

     public int CountStudent() 
     { 
      return entities.STUDENT_CRS_HIST.Count(); 
     } 

     public void Dispose() 
     { 
      entities.Dispose(); 
     }  
    } 

    public class PagedStudentModel 
    { 
     public int TotalRows { get; set; } 
     public IEnumerable<STUDENT_CRS_HIST> Studentcrshist { get; set; } 
     public int PageSize { get; set; } 
    } 
} 

Czy ktoś może mi powiedzieć, co robię źle?

+3

'@ Html.Partial ("~/Views/Sortowanie/_Grid.cshtml", nowy JHelpWebTest2.Models.PagedStudentModel()) ' – DLeh

+0

@DLeh Dzięki, że pracował dla mnie. – hollyquinn

Odpowiedz

8

Jeśli nie udostępnisz modelu częściowego, MVC myśli, że wysyłasz aktualny. Podaj typ modelu, jakiego oczekuje, i powinien działać. Jak Chris Pratt zauważył, wygląda jak należy użyć jeden w swojej klasie CombinedModels

@Html.Partial("~/Views/Sort/_Grid.cshtml", Model.PagedStudentModel) 
+2

Ponieważ PO rzeczywiście ma instancję 'PagedStudentModel' na swoim modelu widoku, najlepiej byłoby użyć następującego:' @ Html.Partial ("~/Views/Sort/_Grid.cshtml", Model.PagedStudentModel) ' –