简体   繁体   English

Asp.Net MVC编辑器模板和无限滚动

[英]Asp.Net MVC Editor Template and Infinite Scrolling

I have following Editor Template for a Table Data as MyProjectViewModel.cshtml 我有以下用于表数据的编辑器模板,作为MyProjectViewModel.cshtml

<div>
    <div>
        @Html.CheckBoxFor(model => model.Selected)
    </div>
    <div>
            for (int i = 1; i < @Model.TypeList.Count; i++)
            {
                    <p id="@Model.TypeList[i].ID" >@Model.TypeList[i].Name</p>
            }
    </div>
        @Html.HiddenFor(model => model.ID)
        @Html.HiddenFor(model => model.Name)
</div>

And I have another view where it is used to post the selected Item, which has the following statement to render the editor template. 我还有另一个视图,该视图用于发布选定的项目,该视图具有以下语句来呈现编辑器模板。

<div id="loadDynamic">
@Html.EditorFor(model => model.NewItem)
</div>

If i post i am able to get the selected items. 如果我张贴,我就能得到选定的物品。

However now I have an issue because this data has to render dynamically on a button click. 但是现在我有一个问题,因为此数据必须在单击按钮时动态呈现。 Onload I will be populating the first 10 items and displaying using the editor Template. 加载时,我将填充前10个项目并使用编辑器模板显示。

However I am stuck how can I get the next 10 data using Ajax and append as part of the above Editor Template. 但是我被困在如何使用Ajax获取下10个数据并将其附加为上述编辑器模板的一部分。

I have created following Action and Ajax however it does't seem to be the proper one. 我创建了以下Action和Ajax,但它似乎不合适。

Action: 行动:

public ActionResult InfinateScroll(string lastID, string prjID)
{
   //Querying DB to get List<Model>
   //Not sure how to get the Editor Template here
   // Tried with following, though it is not the one I needed
   return Json(PartialView("~/Views/MyView/EditorTemplates/MyProjectViewModel.cshtml", model[0]));
}

Ajax: 阿贾克斯:

$a.ajax({
                type: "POST",
                url: '@Url.Action("InfinateScroll", "Project")',
                data: values,
                traditional: true,
                dataType: 'html',
                cache: false,
                success: function (data) {
                    $("#loadDynamic").append(data);
                    $("#loadingDiv").hide();
                },
});

Here even if I am able to append how likely that I will get both the previously selected items and the new one while posting. 即使在这里我可以追加在发布时同时获得先前选择的项目和新获得的项目的可能性。 Please guide me. 请指导我。

The only problem I see with the code you've provided is that you're trying to return the partial view as JSON. 我看到的与您提供的代码有关的唯一问题是,您试图将部分视图返回为JSON。 Remove the call to Json , and just return PartialView(...) . 删除对Json的调用,只返回PartialView(...) Then, you should be golden. 那你应该是金

As far as not returning duplicates goes, you need to treat this as an actual pager. 至于不返回重复项,则需要将其视为实际的寻呼机。 Infinite scrolls don't just return 10 new random items, they page the data without using paging links. 无限滚动不仅会返回10个新的随机项,而且还会在不使用分页链接的情况下分页数据。 So, when you send your AJAX request, you'll need to pass along the current "page" you're on, and use that determine how to dice the data for the return. 因此,在发送AJAX请求时,您需要传递当前所在的“页面”,并使用该页面确定如何将数据切成小块以返回。 You can do it manually easily enough using LINQ helpers like Skip and Take , but you may find it easier to employ a third-party library like PagedList . 您可以使用诸如Skip and Take类的LINQ帮助程序轻松地手动完成此操作,但是您可能会发现使用诸如PagedList之类的第三方库更容易。 It's up to you. 由你决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM