0

I have a problem with calling my function that show the books for same author, for example I have a these authors :

Author Name : Jeremy McPeak; Joe Fawcett; Nicholas C. Zakas;

When I click on "Jeremy McPeak" and "Joe Fawcett" the new page opened well and show all the books for those authors .

But when click on this author "Nicholas C. Zakas" and since he has a DOT in his name , I got exception

Object reference not set to an instance of an object.

This problem only when I click on any author that have a DOT In his name ..

also here is my function :

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

Author View Code:

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

Calling Area :

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

Any one know how to fix that ?

F Msw
  • 133
  • 1
  • 12
  • Updated , have a look . Thanks – F Msw May 28 '16 at 18:22
  • How do you pass AuthorName to Author action? – Ali Soltani May 28 '16 at 18:41
  • Pass the ID of the author, not the name. –  May 28 '16 at 22:33
  • i have no id for the author , just get all the authors as a sting like : Stephen,firas and then split depend on the comma , then using for loop to show each name alone ... – F Msw May 28 '16 at 23:20
  • That approach will likely lead to other issues and I strongly recommend you create a relational table for the Authors. Dots in urls are also going to cause problems (refer [this question/answer](http://stackoverflow.com/questions/11728846/dots-in-url-causes-404-with-asp-net-mvc-and-iis)). As an interim workaround, you could use the index of the author in the array you have created using `String.Split()`. –  May 29 '16 at 00:29

1 Answers1

0

I test code in Author action without return View(result.ToPagedList(pageNumber, pageSize)); There is no problem.I think, ToPagedList has caused an error. result.ToPagedList(pageNumber, pageSize) is null. You trace it. enter image description here

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55