System.Web.Mvc.WebViewPage<TModel>.Model.get returned null in MVC

Viewed 21

This question is similar to the one 5 years ago. I have a system that has an issue with the front end pagination. The backend is fixed and has no errors.

This is the controller:

 // GET: Home
    public ActionResult Index()
    {
        return View(this.GetAllNSLogDetails(1, 1));
    }

    [HttpPost]
    public ActionResult Index(int currentPageIndex)
    {
        return View(this.GetAllNSLogDetails(currentPageIndex, 1));
    }


    [CustomSecurityAttributes]
    public ActionResult GetAllNSLogDetails(int UserID,int page)
    {
        JsonResult _json = new JsonResult();
        NonconformingServiceManager _NSManager = new NonconformingServiceManager();
        _json.Data = _NSManager.GetAllNSIssue(UserID, page); 
        _json.MaxJsonLength = Int32.MaxValue;
        return View(_json);
    }
    
    `public List<NonconformingServiceModel> GetAllNSIssue(int UserID, int currentPage)
    {
        int maxRows = 10;
        using (SIRManagementSystemEntities _entities = new SIRManagementSystemEntities())
        {
            NSLog NSModel = new NSLog();
            var date1 = new DateTime(2000, 1, 1, 1, 0, 0);
            List<NonconformingServiceModel> result = new List<NonconformingServiceModel>();

            var userData = (from _user in _entities.Administrators where _user.UserID == UserID select _user).FirstOrDefault();

            if (userData != null)
            {
                if (userData.UserType == 1 || userData.UserType == 0 || userData.UserID == 38)
                {
                    result = (from _NSDetails in _entities.NonconformingServices
                              join _creator in _entities.Administrators on _NSDetails.CreatedBy equals _creator.UserID
                              join _SIRDetails in _entities.SIR_InitialDetail on _NSDetails.NS_ID equals _SIRDetails.NSCCRefID into _sir
                              from _SIRDetails in _sir.DefaultIfEmpty()
                              select new NonconformingServiceModel
                              {
                                  ID = _NSDetails.NS_ID,
                                  Type = _NSDetails.Type,
                                  Category = _NSDetails.Category,
                                  Reason = _NSDetails.Reason,
                                  Remarks = _NSDetails.Remarks,
                                  ModeThru = _NSDetails.Mode,
                                  DateDelivered = _NSDetails.DateDelivered,
                                  LoadingDate = _NSDetails.LoadingDate,
                                  DateIssue = _NSDetails.DateIssue,
                                  Origin = _NSDetails.Origin,
                                  Destination = _NSDetails.Destination,
                                  CartonNumber = _NSDetails.CartonNumber,
                                  ShipmentNumber = _NSDetails.ShipmentNumber,
                                  ShipperName = _NSDetails.ShipperName,
                                  ConsigneeName = _NSDetails.ConsigneeName,
                                  Address = _NSDetails.Address,
                                  NSGroup = _NSDetails.NSGroup,
                                  NSCode = _NSDetails.NSCode,
                                  OtherType = _NSDetails.OtherType,
                                  CreatedByID = _NSDetails.CreatedBy,
                                  CreatedByName = _creator.FirstName + " " + _creator.LastName,
                                  SIRCode = _SIRDetails.Code == null ? "" : _SIRDetails.Code,
                                  SIRDateIssue = _SIRDetails.IssueDate == null ? date1 : _SIRDetails.IssueDate,
                                  SIRDueDate = _SIRDetails.DueDate == null ? date1 : _SIRDetails.DueDate,
                                  Status = _SIRDetails.Code == null ? 0 : _SIRDetails.Status
                              }).ToList();

                }
                else
                {
                    result = (from _creator in _entities.Administrators
                              join _NSDetails in _entities.NonconformingServices on _creator.UserID equals _NSDetails.CreatedBy
                              join _mapping in _entities.CCNSDEPTMappings on _NSDetails.NSCode equals _mapping.NSCode
                              join _SIRDetails in _entities.SIR_InitialDetail on _NSDetails.NS_ID equals _SIRDetails.NSCCRefID into _sir
                              from _SIRDetails in _sir.DefaultIfEmpty()
                              where _creator.Department == userData.Department || _mapping.ConcernDeptID == userData.Department || _creator.Department == userData.SecondDept || _mapping.ConcernDeptID == userData.SecondDept
                              select new NonconformingServiceModel
                              {
                                  ID = _NSDetails.NS_ID,
                                  Type = _NSDetails.Type,
                                  Category = _NSDetails.Category,
                                  Reason = _NSDetails.Reason,
                                  Remarks = _NSDetails.Remarks,
                                  ModeThru = _NSDetails.Mode,
                                  DateDelivered = _NSDetails.DateDelivered,
                                  LoadingDate = _NSDetails.LoadingDate,
                                  DateIssue = _NSDetails.DateIssue,
                                  Origin = _NSDetails.Origin,
                                  Destination = _NSDetails.Destination,
                                  CartonNumber = _NSDetails.CartonNumber,
                                  ShipmentNumber = _NSDetails.ShipmentNumber,
                                  ShipperName = _NSDetails.ShipperName,
                                  ConsigneeName = _NSDetails.ConsigneeName,
                                  Address = _NSDetails.Address,
                                  NSGroup = _NSDetails.NSGroup,
                                  NSCode = _NSDetails.NSCode,
                                  OtherType = _NSDetails.OtherType,
                                  CreatedByID = _NSDetails.CreatedBy,
                                  CreatedByName = _creator.FirstName + " " + _creator.LastName,
                                  SIRCode = _SIRDetails.Code == null ? "" : _SIRDetails.Code,
                                  SIRDateIssue = _SIRDetails.IssueDate == null ? date1 : _SIRDetails.IssueDate,
                                  SIRDueDate = _SIRDetails.DueDate == null ? date1 : _SIRDetails.DueDate,
                                  Status = _SIRDetails.Code == null ? 0 : _SIRDetails.Status
                              }).ToList();

                    //   var  _result = (List<NonconformingServiceModel>)result.GroupBy(x => x.NSCode).Select(x => x.FirstOrDefault()).ToList().Skip((pageNumber - 1) * 10).Take(10);
                }
                
                var _result = result.GroupBy(x => x.NSCode).Select(x => x.FirstOrDefault()).Skip((currentPage - 1) * maxRows).Take(maxRows).ToList();
                   
                foreach (var item in _result)
                {
                    if (item.Status == null || item.Status == 0)
                    {
                        if (item.SIRCode != "")
                        {
                            DateTime dueDate = item.SIRDueDate.Value;
                            DateTime now = DateTime.Now;
                            if (now.Date > dueDate.Date)
                            {
                                item.FormatIssueDate = "late";
                            }
                            else
                            {
                                item.FormatIssueDate = "not";
                            }
                        }
                        else
                        {
                            item.SIRCode = "";
                            item.SIRDateIssue = null;
                            item.SIRDueDate = null;
                            item.Status = 0;
                        }
                    }
                    else
                    {
                        item.SIRID = (from _sirDet in _entities.SIR_InitialDetail where _sirDet.Code == item.SIRCode select _sirDet.sirID).FirstOrDefault();
                        item.SubmissionDate = (from _sirDet in _entities.SIR_InitialDetail where _sirDet.sirID == item.SIRID select _sirDet.SubmissionDate).FirstOrDefault();
                        item.CEDueDate = (from _sirCA in _entities.SIR_CorrectActDetail where _sirCA.Cor_SIRID == item.SIRID select _sirCA.CEDueDate).FirstOrDefault();
                        item.FormatIssueDate = "submitted";
                    }
                    item.TypeName = (from _type in _entities.DDNSTypes where _type.TypeID == item.Type select _type.Label).FirstOrDefault();
                    if (item.Type == 19)
                    {
                        item.TypeName = item.OtherType;
                    }
                    item.CategoryName = (from _cat in _entities.DDCategories where _cat.SIRCAT_ID == item.Category select _cat.Label).FirstOrDefault();

                    var deptArray = (from _mapping in _entities.CCNSDEPTMappings
                                     join _dept in _entities.Departments on _mapping.ConcernDeptID equals _dept.DeptID
                                     where _mapping.NSCode == item.NSCode
                                     select _dept.DeptID).ToList();

                    var deptCode = (from _mapping in _entities.CCNSDEPTMappings
                                    join _dept in _entities.Departments on _mapping.ConcernDeptID equals _dept.DeptID
                                    where _mapping.NSCode == item.NSCode
                                    select _dept.DeptCode).ToList();

                    item.ConcernedDeptArray = deptArray;
                    item.ConcernedDeptName = deptCode;

                    item.Company = (from _user in _entities.Administrators
                                    join _dept in _entities.Departments on _user.Department equals _dept.DeptID
                                    where _user.UserID == item.CreatedByID
                                    select _dept.Company).FirstOrDefault();
                }


                double pageCount = (double)((decimal)_result.Count() / Convert.ToDecimal(maxRows));
                NSModel.PageCount = (int)Math.Ceiling(pageCount);
                NSModel.CurrentPageIndex = currentPage;
                return _result.ToList();
            }
            return null;

        }
    }`

This is the view model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIMS.Model.Models
{
    public class NonconformingServiceModel
    {

        public int ID { get; set; }
        public byte Type { get; set; }
        public string TypeName { get; set; }
        public string OtherType { get; set; }
        public byte Category { get; set; }
        public string CategoryName { get; set; }
        public string OtherCategory { get; set; }
        public string Reason { get; set; }
        public string Remarks { get; set; }
        public string ModeThru { get; set; }
        public DateTime? DateDelivered { get; set; }
        public DateTime? LoadingDate { get; set; }
        public DateTime DateIssue { get; set; }
        public string ConcernedDeptString { get; set; }
        public List<int> ConcernedDeptArray { get; set; }
        public List<string> ConcernedDeptName { get; set; }
        public string Origin { get; set; }
        public string Destination { get; set; }
        public string CartonNumber { get; set; }
        public string ShipmentNumber { get; set; }
        public string ShipperName { get; set; }
        public string ConsigneeName { get; set; }
        public string Address { get; set; }
        public string NSGroup { get; set; }
        public int CreatedByID { get; set; }
        public string CreatedByName { get; set; }
        public int CreatedByDept { get; set; }
        public int UserType { get; set; }
        public string NSCode { get; set; }
        public byte? Status { get; set; }
        public int? SIRID { get; set; }
        public string SIRCode { get; set; }
        public DateTime? SIRDateIssue { get; set; }
        public DateTime? SIRDueDate { get; set; }
        public DateTime? SubmissionDate { get; set; }
        public DateTime? CEDueDate { get; set; }
        public DateTime? DetectionDate { get; set; }
        public string FormatIssueDate { get; set; }
        public int UserID { get; set; }
        public int? Company { get; set; }
    }

    public class FilterModel
    {
        public string Month { get; set; }
        public string Year { get; set; }
        public byte? Type { get; set; }
        public byte? Category { get; set; }
        public byte? Department { get; set; }
        public byte? Status { get; set; }
    }

    public class NSLog
    {
        
        public List<NonconformingServiceModel> Customers { get; set; }

        public int CurrentPageIndex { get; set; }

        public int PageCount { get; set; }


    }
}

And this is the view:

@model SIMS.Model.Models.NSLog




@{
    ViewBag.Title = "Nonconforming Services Log - SIR";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@section css {
    <!-- Custom styles Datatables -->
    <link href="~/Assets/vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet" />

}
<!-- Begin Page Content -->
<div class="container-fluid">

    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="/Home/Dashboard">Dashboard</a></li>
            <li class="breadcrumb-item active" aria-current="page">Nonconforming Service</li>
        </ol>
    </nav>

    <div class="card shadow mb-4">
        <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
            <h6 class="m-0 font-weight-bold text-primary">Nonconforming Services Log</h6>
            <div class="text-right">
                <button type="button" class="btn btn-outline-info" onclick="window.location.reload();">Reset Filters</button>
                @*<button type="button" data-toggle="modal" data-target=".modal-filter-table" class="btn btn-outline-info"><i class="fas fa-filter"></i>Sort by</button>*@
                <button type="button" onclick="CreateNewConcern()" class="btn btn-outline-primary"><i class="far fa-plus-square"></i> Create New NS</button>
            </div>
        </div>


        <!--CHECKBOX WITH TEXTBOX FILTERING-->
        <!--END OF CHECKBOX WITH TEXTBOX FILTERING-->


        <div class="card-body">
            <div class="card-body table-responsive">
                <table id="NonconformingServiceLogGrid" class="table table-hover" style="width:100%">
                    <thead>
                        <tr>
                            <th>Date Created</th>
                            <th>NS Code</th>
                            <th>SIR Code</th>
                            <th>Date Issue</th>
                            <th>Due Date</th>
                            <th>Submission Date</th>
                            <th>Type</th>
                            <th>Category</th>
                            <th>Concerned Dept.</th>
                            <th>SIR Status</th>
                            <th>CE Due Date</th>
                        </tr>
                    </thead>
                    <tfoot>
                        <tr>
                            <th>Date Created</th>
                            <th>NS Code</th>
                            <th>SIR Code</th>
                            <th>Date Issue</th>
                            <th>Due Date</th>
                            <th>Submission Date</th>
                            <th>Type</th>
                            <th>Category</th>
                            <th>Concerned Dept.</th>
                            <th>SIR Status</th>
                            <th>CE Due Date</th>
                        </tr>
                    </tfoot>

                </table>

                <!--PAGINATION USING PAGEDLIST-->
                <div>
                    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
                    {
                        <table cellpadding="0" cellspacing="0">
                            <tr>
                                @for (int i = 1; i <= Model.PageCount; i++)
                                {
                                    <td>
                                        @if (i != Model.CurrentPageIndex)
                                        {
                                            <a href="javascript:PagerClick(@i);">@i</a>
                                        }
                                        else
                                        {
                                            <span>@i</span>
                                        }
                                    </td>
                                }
                            </tr>
                        </table>
                        <input type="hidden" id="hfCurrentPageIndex" name="currentPageIndex" />
                    }
                    <script type="text/javascript">
                        function PagerClick(index) {
                            document.getElementById("hfCurrentPageIndex").value = index;
                            document.forms[0].submit();
                        }
                    </script>
                </div>
                
                

            </div>
        </div>
    </div>

</div>

If I run the code, I always get the null exception in Model.PageCount in the view.

How do I fix this? All answers are appreciated.

0 Answers
Related