部分视图中的不同模型

时间:2015-04-15 19:59:00

标签: c# asp.net-mvc asp.net-mvc-4 partial-views asp.net-mvc-partialview

我有一个局部视图使用的模型与我正在渲染它的视图不同。我一直收到错误信息。

  

传递到字典中的模型项的类型为“JHelpWebTest2.Models.CombinedModels”,但此字典需要“JHelpWebTest2.Models.PagedStudentModel”类型的模型项。

我不确定如何解决这个问题,这是我的一些代码。 我的索引视图:

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

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

这是我的_Grid局部视图

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

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

这是我的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; }
    }
}

谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:8)

如果您未向Partial提供模型,MVC会认为您正在发送当前模型。给它预期的模型类型,它应该工作。正如Chris Pratt指出的那样,看起来你应该使用CombinedModels类中的那个

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