gpt4 book ai didi

c# - 无法创建接口(interface)的实例 (PagedList)

转载 作者:行者123 更新时间:2023-12-02 21:46:57 26 4
gpt4 key购买 nike

我有一个 View 模型对象,其中包含 List另一种模型对象类型。当用户在页面上进行查询时,如果List返回包含超过 300 条记录,我们希望使用分页来降低加载时​​间(某些搜索结果可以返回超过 14k 记录)。我们正在使用的分页插件can be found here.

结果显示在页面上后,用户可以单击特定结果旁边的复选框,在输入文本框中输入一些信息,点击提交,然后使用该信息编辑所选记录从文本框中。

因为我们需要使用 IPagedList<>但是,为了启用分页,当您点击提交(甚至在页面点击 Controller 之前),我们会收到以下错误:

                 Cannot create an instance of an interface.

查看模型

这是我们用于分页的两个列表对象。 zipCodeTerritory对象保存查询的结果。 pagedTerritoryList用于仅显示用户所在的特定页面上的结果。

    //Paging List objects
public IPagedList<ZipCodeTerritory> pagedTerritoryList { get; set; }
public List<ZipCodeTerritory> zipCodeTerritory { get; set; }
public IPagedList PagingMetaData { get; set; }

Controller

这是我们的基本搜索。 .ToPagedList方法用于指定我们要显示的结果范围并将它们放置在 pagedTerritoryList 中目的。

                //set Paged List counter variables
int pageNumber = page ?? 1;
int pageSize = 300;

//Determine if Territory present?
if (string.IsNullOrWhiteSpace(search.searchTerritory))
{
//State Code ONLY search
search.zipCodeTerritory = (from z in db.ZipCodeTerritory
where z.StateCode.Equals(search.searchState)
select z).ToList();
}
else if(string.IsNullOrWhiteSpace(search.searchState))
{
//Territory ONLY search
search.zipCodeTerritory = (from z in db.ZipCodeTerritory
where z.IndDistrnId.Equals(search.searchTerritory)
select z).ToList();
}
else
{
//Territory AND state search
search.zipCodeTerritory = (from z in db.ZipCodeTerritory
where z.IndDistrnId.Equals(search.searchTerritory) &&
z.StateCode.Equals(search.searchState)
select z).ToList();
}

//Convert list to IPagedList for pagining on Index
search.pagedTerritoryList = search.zipCodeTerritory.ToPagedList(pageNumber, pageSize);

//Set Paged List objects
search.PagingMetaData = new StaticPagedList<ZipCodeTerritory>(search.zipCodeTerritory, pageNumber, pageSize,
search.zipCodeTerritory.Count).GetMetaData();

return View(search);

查看

这是显示搜索结果的表单。如果用户选中该复选框,则点击 clonedelete按钮,结果应该被发送回 Controller 的 Update方法并执行适当的编辑或删除。用户想要在编辑中叠加的信息被输入到newTerritory/Description/etc中。表单中的字段(table 上方)。

关于@Html.PagedListPager我发现我必须从页面将相同的搜索条件传递回索引方法,因此 RouteValueDictionary 中的参数过多。 。

@if (Model.zipCodeTerritory.Count > 0)
{
using (Html.BeginForm("Update", "ZipCodeTerritory", FormMethod.Post))
{
@Html.HiddenFor(model => model.searchZip)
@Html.HiddenFor(model => model.searchDate)
@Html.HiddenFor(model => model.searchState)

<div id="cloneBox">
<div id="rw1">
@Html.LabelFor(model => model.newTerritory)
@Html.TextBoxFor(model => model.newTerritory, new { style = "width: 30px;padding-left:10px;", maxLength = 3 })
@Html.LabelFor(model => model.newDescription)
@Html.TextBoxFor(model => model.newDescription, new { style = "width: 250px;padding-left:10px;", maxLength = 30 })
@Html.LabelFor(model => model.newEffectiveDate)
@Html.TextBoxFor(model => model.newEffectiveDate, new { style = "width: 80px;padding-left:10px;" })
<div id="rw2" style="padding-top: 10px;">
@Html.LabelFor(model => model.newChannelCode)
@Html.DropDownListFor(model => model.newChannelCode, Model.ChannelCodes, " ")
@Html.LabelFor(model => model.newStateCode)
@Html.DropDownListFor(model => model.newStateCode, Model.StateCodes, " ")
</div>
</div>
</div>
<br/>
<div id="buttonDiv">
<button type="submit" id="CloneButton" name="button" value="clone">Apply New Data</button>
<button type="submit" id="deleteButton" name="button" value="delete">Delete Selected Items</button>
</div>


@*Display paging only if necessary*@
if (Model.pagedTerritoryList.Count >= 300)
{
<div id="pagingDiv">
@Html.PagedListPager(new StaticPagedList<Monet.Models.ZipCodeTerritory>(Model.zipCodeTerritory, Model.PagingMetaData) ,
Page => Url.Action("Index", new RouteValueDictionary()
{
{ "Page", Page},
{ "searchZip", Model.searchZip },
{ "searchActiveOnly", Model.searchActiveOnly },
{ "searchDate", Model.searchDate },
{ "searchState", Model.searchState },
{ "searchTerritory", Model.searchTerritory },
{ "searchChannel" , Model.searchChannelCode }
}), PagedListRenderOptions.DefaultPlusFirstAndLast)
</div>
}

<table id="thetable" class="tablesorter" >
<thead>
<th>@Html.CheckBox("SelectAll")</th>
<th>State</th>
<th>Territory</th>
<th>Zip</th>
<th>Description</th>
<th>Effective</th>
<th>End Date</th>
<th>Last Update Date</th>
<th>Channel</th>
<th></th>
</thead>
<tbody id="tableBody">
@for (int i = 0; i < Model.pagedTerritoryList.Count; i++)
{
<tr id="@(Model.lastEditedId == Model.pagedTerritoryList[i].Id ? "lastEdit" : "")">
<td>
@Html.CheckBoxFor(model => model.pagedTerritoryList[i].Update)
@Html.HiddenFor(model => model.pagedTerritoryList[i].Update)
</td>
<td>
@Html.DisplayFor(model => model.pagedTerritoryList[i].StateCode)
@Html.HiddenFor(model => model.pagedTerritoryList[i].StateCode)
</td>
<td>
@Html.DisplayFor(model => model.pagedTerritoryList[i].IndDistrnId)
@Html.HiddenFor(model => model.pagedTerritoryList[i].IndDistrnId)
</td>
<td>
@Html.DisplayFor(model => model.pagedTerritoryList[i].ZipCode)
@Html.HiddenFor(model => model.zipCodeTerritory[i].ZipCode)
</td>
<td>
@Html.DisplayFor(model => model.pagedTerritoryList[i].DrmTerrDesc)
@Html.HiddenFor(model => model.pagedTerritoryList[i].DrmTerrDesc)
</td>
<td>
@Html.DisplayFor(model => model.pagedTerritoryList[i].EffectiveDate)
@Html.HiddenFor(model => model.pagedTerritoryList[i].EffectiveDate)
</td>
<td>
@if (Model.pagedTerritoryList[i].EndDate.Date != DateTime.MaxValue.Date)
{
@Html.DisplayFor(model => model.pagedTerritoryList[i].EndDate)
@Html.HiddenFor(model => model.pagedTerritoryList[i].EndDate)
}
</td>
<td>
@Html.DisplayFor(model => model.pagedTerritoryList[i].LastUpdateDate)
@Html.HiddenFor(model => model.pagedTerritoryList[i].LastUpdateDate)
</td>
<td>
@Html.DisplayFor(model => model.pagedTerritoryList[i].ChannelCode)
@Html.HiddenFor(model => model.pagedTerritoryList[i].ChannelCode)
</td>

@if (ViewBag.SecurityLevel >= 4)
{
<td>
@Html.ActionLink("Edit", "Edit", new
{
id = Model.zipCodeTerritory[i].Id,
searchZip = Model.searchZip,
searchActiveOnly = Model.searchActiveOnly,
searchDate = Model.searchDate,
searchState = Model.searchState,
searchTerritory = Model.searchTerritory,
searchChannelCode = Model.searchChannelCode
})
@Html.HiddenFor(model => model.zipCodeTerritory[i].Id)
</td>
}

</tr>
}
</tbody>
</table>
}
}

编辑

根据下面的评论,这是表单发布到的方法的签名。它包含 ZipCodeIndex 的实例最初加载到页面上的内容,加上 button 中的文本确定我们是否正在执行 clonedelete

    [HttpPost]
public ActionResult Update(ZipCodeIndex updateZip, string button)
{

第二次编辑

尝试了this question中的方法但仍然收到原始错误消息(“无法创建接口(interface)实例”)。

最佳答案

我能够完全摆脱这个问题,但我不认为这是最好的解决方案。如果有人能提供更好的答案,我会很高兴,但我会同时将其发布在这里。

IPagedList对象被构建为保存特定范围的 List<>我刚刚在我的 View 模型上创建了一个显示属性,并在我的 View 上使用了它。这个列表,不是IPagedList被发送回 Controller 以进行更新,因此不会发生界面怪异。

查看模型

    //Paging List objects
public IPagedList<ZipCodeTerritory> pagedTerritoryList { get; set; }
public List<ZipCodeTerritory> zipCodeTerritory { get; set; }
public List<ZipCodeTerritory> displayForPaging { get; set; }

Controller

    //Convert list to IPagedList for pagining on Index
search.pagedTerritoryList = search.zipCodeTerritory.ToPagedList(pageNumber, pageSize);
search.displayForPaging = search.pagedTerritoryList.ToList();

查看

    <td>
@Html.CheckBoxFor(model => model.displayForPaging[i].Update)
@Html.HiddenFor(model => model.displayForPaging[i].Update)
</td>
<td>
.
.

关于c# - 无法创建接口(interface)的实例 (PagedList),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19366673/

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