gpt4 book ai didi

c# - 局部 View 中的不同模型

转载 作者:太空狗 更新时间:2023-10-29 22:36:52 26 4
gpt4 key购买 nike

我有一个局部 View 使用的模型与我在其中呈现它的 View 不同。我不断收到错误消息。

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'.

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

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

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

这是我的 _Grid 部分 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>

这是组合模型:

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

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

最佳答案

当您不向分部提供模型时,MVC 认为您发送的是当前模型。给它期望的模型类型,它应该可以工作。正如 Chris Pratt 指出的那样,看起来你应该在你的 CombinedModels 类中使用那个

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

关于c# - 局部 View 中的不同模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29659727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com