0

I just installed the nuget PagedList for the first time based on the tutorial provided by Microsoft but I get the following error:

"System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List'1[BOL4.EventCalendar]', but this dictionary requires a model item of type 'PagedList.IPagedList'1[BOL4.EventCalendar]'." after submitting a new row from Add Popup. The row is inserted in the database.

Here is my code for it:

DentAppEntities db = new DentAppEntities();


    [HttpGet]
    public ActionResult Add()
    {
        return PartialView();
    }

    [HttpPost]
    public ActionResult Add(BOL4.EventCalendar appc)
    {
        if (ModelState.IsValid)
        {
            db.EventCalendars.Add(appc);
            db.SaveChanges();
        }
        return View("List", db.EventCalendars.ToList());
    }

here is my List View:

@model PagedList.IPagedList<BOL4.EventCalendar>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "List";
}
<h2>Home Page</h2>
<p>
    <a href="@Url.Action("Add", "Home")" id="Add" class="btn btn-primary"> Add</a>
</p>    
<div class="table-responsive">
    <table>
        <thead>
            <tr">
                <th>
                    @Html.ActionLink("Title", "List", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
                </th>
                <th>
                    @Html.ActionLink("Description", "List", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
                </th>
                <th>
                    @Html.ActionLink("StartAt", "List", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
                </th>
                <th>
                    @Html.ActionLink("EndAt", "List", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
                </th>
                <th>
                    @Html.ActionLink("IsFullDay", "List", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Title)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Description)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.StartAt)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.EndAt)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.IsFullDay)
                    </td>
                    <td>
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <br />
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

    @Html.PagedListPager(Model, page => Url.Action("List",
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
</div>

<div class="modal fade" id="Add-Model" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Add Event</h4>
            </div>
            <div class="divForAdd">
            </div>
        </div>
    </div>
</div>    

<script>
    $(document).ready(function () {
        $('#Add').click(function (event) {
            event.preventDefault();
            $.get(this.href, function (response) {
                $('.divForAdd').html(response);
            });
            $('#Add-Model').modal({
                backdrop: 'static',
            }, 'show');
            return false;
        });           
    });
</script>

and here is the Add partial view:

@model BOL4.EventCalendar

@using (Ajax.BeginForm("Add", "Home", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "div-record", OnSuccess = "$('.close').click()" }))
{
    //Textboxes here
    <div class="modal-footer">
        <button type="submit" class="btn btn-success" name="cmd">Save</button>
    </div>

}

Also Here is Index controller:

public ViewResult List(string sortOrder, string currentFilter, string searchString, int? page)
    {
        ViewBag.CurrentSort = sortOrder;
        ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
        ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

        if (searchString != null)
        {
            page = 1;
        }
        else
        {
            searchString = currentFilter;
        }

        ViewBag.CurrentFilter = searchString;

        var eves = from s in db.EventCalendars
                   select s;
        if (!String.IsNullOrEmpty(searchString))
        {
            eves = eves.Where(s => s.Title.Contains(searchString)
                                   || s.Description.Contains(searchString));
        }
        switch (sortOrder)
        {
            case "Title":
                eves = eves.OrderByDescending(s => s.Title);
                break;
            case "Description":
                eves = eves.OrderBy(s => s.Description);
                break;
            default:  // Name ascending 
                eves = eves.OrderBy(s => s.Title);
                break;
        }

        int pageSize = 3;
        int pageNumber = (page ?? 1);
        return View(eves.ToPagedList(pageNumber, pageSize));
    }

Also tried another way, see the following link: After Submit TotalPages Throws error. Can anybody please help me with this issue. Thanks in advance!

Community
  • 1
  • 1
Jon A
  • 137
  • 2
  • 11
  • Certainly your action method returning `List` for a view declared with `IPagedList`. Try using `return View("List", db.EventCalendars.ToPagedList())` on controller action method. – Tetsuya Yamamoto Apr 12 '17 at 08:16
  • Thanks for your replay. i will try it and come bace with a replay. Thanks! – Jon A Apr 12 '17 at 08:17
  • It shows error: "No overload method 'ToPagedList' takes 0 arguments". – Jon A Apr 12 '17 at 08:21
  • Note that `ToPagedList` requires 2 arguments: `pageNumber` and `pageSize`, ensure you've already include them before returning view (I assume you can pass both values through `BOL4.EventCalendar` model). – Tetsuya Yamamoto Apr 12 '17 at 08:24
  • I have a question on this topic, is it possibile to add those two arguments in a add action? and how exactly? Updated my question with the list index view. – Jon A Apr 12 '17 at 08:25
  • Have you mean adding both of them in POST action method? If it's true, it depends on the structure of your model class. Let's go back to the previous one since model item error is exactly a dupe of another similar question. – Tetsuya Yamamoto Apr 12 '17 at 08:29
  • Ok let see on the other topic what is wrong, and thanks again for your time. – Jon A Apr 12 '17 at 08:31
  • @Stephen Muecke can you please explain a bit more about it on my eg. because I don't understand something. Do I need to create another model in which I do something like this: `public class Foo { public Bar MyBar { get; set; } }`? – Jon A Apr 12 '17 at 08:36
  • @JonA, Your question is a code dump (90% of this code is irrelevant). You have not even said where/when the error occurs. –  Apr 12 '17 at 08:40
  • My error occurs after submitting a new row or edit one. And yes it may look like a code dump but I'm still trying to learn because I'm new to programming with asp.net mvc. – Jon A Apr 12 '17 at 08:41
  • Your `Add` POST method passes `List` to a view named `List.cshtml`. Best guess is that view has `@model PagedList.IPagedList` but you have not even shown that view! –  Apr 12 '17 at 08:59
  • Look at the Index part (the 2nd part of code) of the question it's the list view – Jon A Apr 12 '17 at 09:06
  • No where in your question have your shown `List.cshtml`! –  Apr 12 '17 at 09:13
  • After the code part at the begining there is the Index View (that is it). – Jon A Apr 12 '17 at 09:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141525/discussion-between-stephen-muecke-and-jon-a). –  Apr 12 '17 at 09:29

0 Answers0