5
votes

I have a partial view that is using a different model than the view that I'm rendering it in. I keep getting the error message.

The model item passed into the dictionary is of type 'JHelpWebTest2.Models.CombinedModels', but this dictionary requires a model item of type 'JHelpWebTest2.Models.PagedStudentModel'.

I'm not sure how to fix this here is some of my code. My Index view:

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

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

Here is my _Grid partial view

@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>

This is the 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; } 
    }
}

This is my model for 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; }
    }
}

Can anyone tell me what I'm doing wrong?

1
@Html.Partial("~/Views/Sort/_Grid.cshtml", new JHelpWebTest2.Models.PagedStudentModel())DLeh
@DLeh Thanks that worked for me.hollyquinn

1 Answers

8
votes

When you don't provide a model to a Partial, MVC thinks you're sending the current one. Give it the model type it's expecting and it should work. As Chris Pratt pointed out, looks like you should use the one in your CombinedModels class

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