尝试打开/调用功能时出现DOT异常

时间:2016-05-28 17:36:05

标签: asp.net-mvc asp.net-mvc-4

调用我的函数显示同一作者的书籍时遇到问题,例如我有这些作者:

作者姓名:Jeremy McPeak;乔福西特; Nicholas C. Zakas ;

当我点击"Jeremy McPeak""Joe Fawcett"时,新页面打开并显示这些作者的所有书籍。

但是当点击这位作者"Nicholas C. Zakas"并且因为他的名字中有一个DOT时,我得到了例外

  

对象引用未设置为对象的实例。

仅当我点击任何在他的名字中有DOT的作者时才会出现此问题..

这也是我的功能:

public ActionResult Author(string AuthorName, int? page)
{
    if (AuthorName != null)
    {
        ViewBag.AuthorName = AuthorName;
        int pageSize = 6;
        int pageNumber = page ?? 1;
        var result = db.Books.Where(s => s.Author_name.Contains(AuthorName)).OrderByDescending(x => x.Book_id).ToList();
        return View(result.ToPagedList(pageNumber, pageSize));
    }
    return RedirectToAction("Index", "Home");
}

作者查看代码:

@using PagedList.Mvc
@using System.Text.RegularExpressions;
@model PagedList.IPagedList<SmartBookLibrary.Models.Book>
@{
    ViewBag.Title = "Books For " + ViewBag.AuthorName+ "- Smart Books Library";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section AdditionalMeta
{
    <meta name="robots" content="index,follow">
}
<div class="container">
    <!-- Page Header -->
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">                
                <span class="fa fa-pencil"></span>@ViewBag.AuthorName
            </h1>
        </div>
    </div>
    <!-- /.row -->
    <!-- Projects Row -->
    <div class="row">
        @foreach (var item in Model)
        {
            <div class="col-md-4 portfolio-item" style="margin-bottom:10px;height:450px">
                <div class="ih-item square effect3 bottom_to_top">
                    <a href="~/book/@item.Book_id/@item.UrlSlug">
                        <div class="img"><img itemprop="image" class="img-responsive" src="~/Images/@item.Book_Image" alt="@item.Book_name" title="@item.Book_name"></div>
                        <div class="info">
                            <h3>@item.Book_name</h3>
                        </div>
                    </a>
                </div>
                <h3 itemprop="name">
                    <a href="~/book/@item.Book_id/@item.UrlSlug">
                        @item.Book_name
                        @if (item.Edition != 0)
                        {
                            switch (item.Edition)
                            {
                                case 1:<small class="btn btn-default btn-xs">@item.Edition'St Edition</small> break;
                                case 2:<small class="btn btn-default btn-xs">@item.Edition'nd Edition</small> break;
                                case 3:<small class="btn btn-default btn-xs">@item.Edition'rd Edition</small> break;
                                case 4 - 10:<small class="btn btn-default btn-xs">@item.Edition'th Edition</small> break;
                                default:<small class="btn btn-default btn-xs">@item.Edition'th Edition</small> break;

                            }
                        }
                    </a>
                </h3>
                <p itemprop="description">
                    @*@Html.Raw(item.Book_Description)*@
                </p>
            </div>
        }
    </div>
    <!-- /.row -->
    <hr>
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
    @Html.PagedListPager(Model, page => Url.Action("Author", new { AuthorName = ViewBag.AuthorName, page }))
</div>

通话区域:

@{
    var s = Model.Book.Author_name.ToString();
    string[] Auth = s.Split(',');
    ViewBag.Author = Auth;
}
<p itemprop="author">
    <i class="glyphicon glyphicon-pencil"></i> <b>Author Name :</b>
    @for (int i = 0; i < Auth.Length; i++)
    {
        <a style="color:#777777" href="~/Author/@Auth[i]">@Auth[i];</a>
    }
</p>

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我在没有return View(result.ToPagedList(pageNumber, pageSize));的作者操作中测试代码 没问题。我认为,ToPagedList导致了错误。 result.ToPagedList(pageNumber, pageSize)为空。 你跟踪它。 enter image description here