提交TotalPages后引发错误

时间:2017-04-11 11:42:06

标签: ajax asp.net-mvc entity-framework-6

我的分页有点问题(“Double TotalPage = @ ViewBag.TotalPages;在Partial View上”),在Create或Edit上提交后,分页会抛出以下错误:“无法将null转换为'double',因为它是一个不可为空的值类型“。我创建了一个包含列表的部分视图(使用Ajax的Crud操作),一切正常,我可以转到列表中的不同页面,但是当我提交新行或编辑现有行时,它会显示该错误。这是代码:

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());
    }

这是局部视图:

@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>

这是索引视图:

<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>

0 个答案:

没有答案