0

I have a little problem with my paging ("double TotalPage = @ViewBag.TotalPages; on Partial View"), after submiting on Create or Edit the paging throws the following error: "Cannot convert null to 'double' because it is a non-nullable value type". I have created a partial view that contains the list (Crud Operations using Ajax), all works fine, I can go to different pages in the list but when I submit a new row or edit an existing one it displays that error. Here is the code for it:

public ActionResult IndexApp(string Page)
    {
        var appc = objBs.appointmentdiaryBs.GetALL();
        appc = from ac in db.tbl_Appoiment_Diary
             select ac;
        ViewBag.TotalPages = Math.Ceiling(objBs.appointmentdiaryBs.GetALL().Count() / 5.0);
        int page = int.Parse(Page == null ? "1" : Page);
        ViewBag.Page = page;
        appc = appc.Skip((page - 1) * 5).Take(5);
        ViewBag.Count = db.tbl_Appoiment_Diary.SqlQuery("SELECT * FROM dbo.tbl_Appoiment_Diary").Count();
        return View(appc.ToList());
    }

here is the partial view:

@model IEnumerable<BOL3.tbl_Appoiment_Diary>

<div class="table-responsive">
    <table class="table" id="tbl" style="border-collapse: separate; border: solid grey 1px; border-radius: 6px; -moz-border-radius: 6px;">
        <thead>
            <tr style="background-color:aqua">
                <th>
                    @Html.ActionLink("Title", "Title", "IndexApp", new { SortOrder = ViewBag.SortOrder == null ? "Asc" : (ViewBag.SortOrder == "Asc" ? "Des" : "Asc"), SortBy = "Title", page = (ViewBag.Page == null ? "1" : ViewBag.Page) })
                </th>
                <th>
                    @Html.ActionLink("Scheduled Date and Time", "DateTimeScheduled", "IndexApp", new { SortOrder = ViewBag.SortOrder == null ? "Asc" : (ViewBag.SortOrder == "Asc" ? "Des" : "Asc"), SortBy = "DateTimeScheduled", page = (ViewBag.Page == null ? "1" : ViewBag.Page) })
                </th>
                <th>
                    @Html.ActionLink("Appointment Lenght", "AppointmentLenght", "IndexApp", new { SortOrder = ViewBag.SortOrder == null ? "Asc" : (ViewBag.SortOrder == "Asc" ? "Des" : "Asc"), SortBy = "AppointmentLenght", page = (ViewBag.Page == null ? "1" : ViewBag.Page) })
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Title)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.DateTimeScheduled)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.AppointmentLenght)
                    </td>
                    <td style="align-content:center">
                        <a href="@Url.Action("EditApp", "ClientAppoint", new { id = item.ID })" class="editDialog">Edit</a> |
                        @Html.ActionLink("Delete", "Delete", new { id = item.ID })
                    </td>
                </tr>
            }
        </tbody>
    </table>

@{
    double TotalPage = @ViewBag.TotalPages;
}
<ul class="pagination pagination-sm">
    @for (int i = 1; i <= TotalPage; i++)
    {
        if (i == ViewBag.Page)
        {
            <li class="active">@Html.ActionLink(i.ToString() + " ", "IndexApp", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : ViewBag.SortOrder), SortBy = (ViewBag.SortBy == null ? "Title" : ViewBag.SortBy), Page = i })</li>
        }
        else
        {
            <li>@Html.ActionLink(i.ToString() + " ", "IndexApp", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : ViewBag.SortOrder), SortBy = (ViewBag.SortBy == null ? "Title" : ViewBag.SortBy), Page = i })</li>
        }
    }
</ul>
</div>

and here is the Index view:

<div id="main-div">
            <div class="clearfix">&nbsp;</div>
            <div class="clearfix">&nbsp;</div>
            <div class="container">                 
                <a href="@Url.Action("Action", "Controller")" id="Add" class="btn btn-primary btn btn-xs">Adauga</a>
                <br />
                <div id="div-record1">
                    @Html.Partial("_Detail")
                </div>
            </div>
        </div>
Jon A
  • 137
  • 2
  • 11
  • Seems that you need to refresh partial view after create/edit operation due to `ViewBag.TotalPages` has null value at those times. Can you clarify this by mentioning result of this line: `ViewBag.TotalPages = Math.Ceiling(objBs.appointmentdiaryBs.GetALL().Count() / 5.0);`? – Tetsuya Yamamoto Apr 12 '17 at 01:33
  • Thank you for your replay. The count on that line is 0, but if I close the application an then run it again it displays the last value inserted. I tried to do somthing like "return PartialView("_Detail", appc.ToList());", but still didn't work. Thank you again for the info. – Jon A Apr 12 '17 at 07:17
  • Also tried this: `[HttpPost] public ActionResult AddApp(BOL3.tbl_Appoiment_Diary appc) { if (ModelState.IsValid) { db.tbl_Appoiment_Diary.Add(appc); db.SaveChanges(); return PartialView("_Detail",db.tbl_Appoiment_Diary.ToList()); } //return PartialView("_Detail", db.tbl_Appoiment_Diary.ToList()); //return RedirectToAction("IndexApp"); return View(appc); }` – Jon A Apr 12 '17 at 07:39
  • Tried with PagedList but I get another error. See the following link: https://stackoverflow.com/questions/43363881/error-after-submitting-from-a-ajax-form – Jon A Apr 12 '17 at 08:16

0 Answers0