locked
Partial View rendering error using PagedList MVC RRS feed

  • Question

  • User-1248511554 posted

    This is my Index page:

    @model my_filter_model
    
    <link href="~/Content/bootstrap-multiselect.css" rel="stylesheet" />
    
    <div class="page-content">
    
        <div class="filter-container clearfix">
            some combobox filters
                <div class="col">
                    <div class="inputGroupContainer actions">
                        <a class="button right" onclick="load()">Cerca</a>
                        <a class="button right" onclick="reset()">Reset</a>
                    </div>
                </div>
            </div>
        </div>
    
        <div id="filter-result-container"></div>
    </div>
    
    @section Scripts {
        <script type="text/javascript" src="~/Scripts/bootstrap-multiselect.js"></script>
        <script type="text/javascript" src="~/Scripts/alertify.min.js"></script>
        <script>
            
            function load() {
                API_Loading.show();
                var url = BASE_PATH + "class/ApplyFilters";
    
                API_ajaxWrapper_Library.ajax(url,
                    {
                        method: "POST",
                        traditional: true,
                        data: {
                            somefilters: $("#somefilters").val(),
                            pageNumber: 1,
                            pageSize: 100,
                        },
                        onDone: function(data) {
                            $("#filter-result-container").html(data);
                            API_Loading.hide();
                        },
                        onFail: function(result) {
                            API_Loading.hide();
                            alertify.error("Errors");
                        }
                    });
            }    
        </script>
    }
    

    This is ApplyFilters method in controller class:

    public ActionResult ApplyFilters(some_filters_property, int pageNumber, int pageSize)
            {
                var presenter = this.builder.GetBuildingFiltered(some_filters_property, pageNumber, pageSize);
                
                return this.View("~path_of_cshtml_file/_BuildingFoundPartial.cshtml", presenter);
            }
    

    This is GetBuildingFiltered method:

    private BuildingsViewModelPagedList GetBuildingFiltered (some_filters, int pageNumber, int pageSize)
            {
               var list = GetBuildings(some_filters, pageNumber, pageSize).toList(); <-- In this method I convert from IEnumerable with ToPagedList(pageNumber, pageSize)
               
                return new BuildingsViewModelPagedList ()
                {
                    Building = list,
                    PageNumber = pageNumber,
                    PageSize = pageSize,
                    TotalItemCount = list.Count();
                };
            }
    

    This is entity BuildingsViewModelPagedList:

    public class BuildingsViewModelPagedList { public List Building { get; set; }

    public int PageNumber { get; set; }
    
    public int TotalItemCount { get; set; }
    
    public int PageSize { get; set; }
    

    }

    This is the Partial _BuildingFoundPartial;

        @using PagedList.Mvc
    @model BuildingsViewModelPagedList
    
    <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
    
    @{
        Layout = String.Empty;
    }
    
    
    @{
        if (Model.Building.Count() > 0)
        {
            foreach (var building in Model.Building)
            {
                @Html.DisplayFor(x => building); <-- Partial that makes up the Master-Details structure of the elements
            }
    
            <br />
                Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount  <-- this generate error
        }
        else
        {
            <label class="labels">0 Buildings found</label>
        }
    
    }
    

    I would not like the problem of the part of the code where I call @Page is due to the fact that I have a loop that I use to render the Master-Details structure, in the Index I cannot put it because the model is different. I don't get any exceptions, but the page doesn't render

    Tuesday, October 6, 2020 10:39 AM

All replies

  • User-474980206 posted

    As the model does not have a .PageCount property, an error is expected.

    Tuesday, October 6, 2020 2:35 PM
  • User-1248511554 posted

    Now my model has that property but it still doesn't render

    Tuesday, October 6, 2020 2:44 PM
  • User-474980206 posted

    if you use the browsers network trace you can see the error. probably view file not found.

    Tuesday, October 6, 2020 5:57 PM
  • User1312693872 posted

    Hi,shaun87RP

    You mean you can't show the partial view in div?

    You can try to change the ajax like this form and make sure 'somefilters' matches the parameter in controller action:

    <script>
    
            function load() {
                event.preventDefault();
                $.ajax({
                    url: "ApplyFilters",
                    method: "POST",
                    data: {
                        somefilters: 123,
                        pageNumber: 1,
                        pageSize: 100,
                    },
                    success: function (response) {
                        $("#filter-result-container").html(response);
                    }
                });
            }
        </script>

    Controller:

    public ActionResult ApplyFilters(string somefilters, int pageNumber, int pageSize)
            {
                var presenter = GetBuildingFiltered(somefilters, pageNumber, pageSize);
    
                return View("_BuildingFoundPartial", presenter);
            }
    
            private BuildingsViewModelPagedList GetBuildingFiltered(string somefilters, int pageNumber, int pageSize)
            {
                ...
            }

    If this can't solve your problem, you can press F12 and do some trouble shooting.

    Best Regards,

    Jerry Cai

    Wednesday, October 7, 2020 5:52 AM
  • User-1248511554 posted

    I found the problem ... it was trivial at the end, the part where I use PageCount was not inserted in an html tag, I was able to understand it because I printed the response of my call in the console and the error was just that

    Wednesday, October 7, 2020 9:28 AM
  • User1312693872 posted

    Hi,shaun87RP

    Since you have found the error, do you have any trouble solving it?

    Best Regards,

    Jerry Cai

    Thursday, October 8, 2020 6:13 AM