user3147373
user3147373

Reputation: 23

how to solve : foreach statement cannot operate

I have a controller called "BookController" :

public ActionResult Book(int? id)
    {

        var result = db.Books.Where(b => b.Book_id == id).Single();
        var user = db.AspNetUsers.Where(b => b.Id == result.User_ID).Single();
        ViewBag.User_ID = user.UserName;
        return View(result);
    }

public ActionResult SameAuthor(string author)
        {

            var result = db.Books.Where(b => b.Author_name == author).Take(4);
             return View(result.ToList());
        }

also this is the view code "books.cshtml" :

@model SmartBookLibrary.Models.Book
@using SmartBookLibrary.Controllers
@{
    ViewBag.Title = Model.Book_name;
    BookController book = new BookController();
    var auth = Model.Author_name;
}
@section AdditionalMeta
{
<link href="~/Content/blog-post.css" rel="stylesheet" />
<link href="~/Content/font-awesome.min.css" rel="stylesheet" />
}
<div class="container">

    <div class="row">

        <!-- Blog Post Content Column -->
        <div class="col-lg-8">

            <!-- Blog Post -->
            <!-- Title -->
            <h1>@Model.Book_name (@Model.Edition th Edition)  <a href="~/Admin/books/edit/@Model.Book_id"><small class="fa fa-edit"></small></a></h1>

            <!-- Author -->
            <p class="lead">
                by <a href="#">@ViewBag.User_ID</a>
            </p>

            <hr>

            <!-- Date/Time -->
            <p><span class="glyphicon glyphicon-time"></span> Posted on @Model.Publish_date</p>

            <hr>

            <!-- Preview Image -->
            <div class="row">
                <div class="col-md-3">
                    <img class="img-show" src="/Books/RetrieveImage/@Model.Book_id" alt="">
                </div>
                <div class="col-md-6 col-md-offset-1">
                    <dl>Book Name : @Model.Book_name</dl>
                    <dl>Book Edition : @Model.Edition</dl>
                    <dl>Author Name : @Model.Author_name</dl>
                    <dl>Category : @Model.Category.Category_name</dl>
                    <dl>Book Name: @Model.Book_name</dl>
                    <dl>ISN : 2015</dl>
                </div>
            </div>                    

            <hr>

            <!-- Post Content -->

            <p class="lead">@Html.Raw(Model.Book_Description)</p>



            <button class="btn btn-block btn-lg btn-success">
                Download @Model.Book_name
                <i class="fa fa-cloud-download"></i>
            </button>     




            <hr>
            <div class="well">
                <div class="row">
                    @*   I Want to call the contoller method , it will return 4 post , then i want to show them here   *@
                    @{
                        var b = book.SameAuthor(auth);
                        foreach (var cat in b)
                        {
                            <div class="col-sm-6 col-md-3">
                                <div class="thumbnail">
                                    <img src="/Books/RetrieveImage/" alt="Generic placeholder thumbnail">
                                </div>

                                <div class="caption">
                                    <h3>Book_name</h3>
                                    <p>Some sample text. Some sample text.</p>

                                    <p>
                                        <a href="#" class="btn btn-primary" role="button">
                                            Button
                                        </a>

                                        <a href="#" class="btn btn-default" role="button">
                                            Button
                                        </a>
                                    </p>

                                </div>
                            </div>
                        }
                    }


                </div>

            </div>
            <hr>
            <!-- Blog Comments -->
            <!-- Comments Form -->
            <div class="well">
                <h4>Leave a Comment:</h4>
                <form role="form">
                    <div class="form-group">
                        <textarea class="form-control" rows="3"></textarea>
                    </div>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </form>
            </div>

            <hr>

            <!-- Posted Comments -->
            <!-- Comment -->
            <div class="media">
                <a class="pull-left" href="#">
                    <img class="media-object" src="http://placehold.it/64x64" alt="">
                </a>
                <div class="media-body">
                    <h4 class="media-heading">
                        Start Bootstrap
                        <small>August 25, 2014 at 9:30 PM</small>
                    </h4>
                    Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
                </div>
            </div>

            <!-- Comment -->
            <div class="media">
                <a class="pull-left" href="#">
                    <img class="media-object" src="http://placehold.it/64x64" alt="">
                </a>
                <div class="media-body">
                    <h4 class="media-heading">
                        Start Bootstrap
                        <small>August 25, 2014 at 9:30 PM</small>
                    </h4>
                    Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
                    <!-- Nested Comment -->
                    <div class="media">
                        <a class="pull-left" href="#">
                            <img class="media-object" src="http://placehold.it/64x64" alt="">
                        </a>
                        <div class="media-body">
                            <h4 class="media-heading">
                                Nested Start Bootstrap
                                <small>August 25, 2014 at 9:30 PM</small>
                            </h4>
                            Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
                        </div>
                    </div>
                    <!-- End Nested Comment -->
                </div>
            </div>

        </div>

        <!-- Blog Sidebar Widgets Column -->
        <div class="col-md-4">

            <!-- Blog Search Well -->
            <div class="well">
                <h4>Search</h4>
                <div class="input-group">
                    <input type="text" class="form-control">
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="button">
                            <span class="glyphicon glyphicon-search"></span>
                        </button>
                    </span>
                </div>
                <!-- /.input-group -->
            </div>

            <!-- Blog Categories Well -->
            <div class="well">
                <h4>Blog Categories</h4>
                <div class="row">
                    <div class="col-lg-6">
                        <ul class="list-unstyled">
                            <li>
                                <a href="#">Category Name</a>
                            </li>
                            <li>
                                <a href="#">Category Name</a>
                            </li>
                            <li>
                                <a href="#">Category Name</a>
                            </li>
                            <li>
                                <a href="#">Category Name</a>
                            </li>
                        </ul>
                    </div>
                    <div class="col-lg-6">
                        <ul class="list-unstyled">
                            <li>
                                <a href="#">Category Name</a>
                            </li>
                            <li>
                                <a href="#">Category Name</a>
                            </li>
                            <li>
                                <a href="#">Category Name</a>
                            </li>
                            <li>
                                <a href="#">Category Name</a>
                            </li>
                        </ul>
                    </div>
                </div>
                <!-- /.row -->
            </div>

            <!-- Side Widget Well -->
            <div class="well">
                <h4>Books May You Interest</h4>
            </div>

        </div>

    </div>
    <!-- /.row -->
</div>

my problem actually here in this code "in books.cshtml":

 @{
                        var b = book.SameAuthor(auth);
                        foreach (var cat in b)
                        {
                            <div class="col-sm-6 col-md-3">
                                <div class="thumbnail">
                                    <img src="/Books/RetrieveImage/" alt="Generic placeholder thumbnail">
                                </div>

                                <div class="caption">
                                    <h3>Book_name</h3>
                                    <p>Some sample text. Some sample text.</p>

                                    <p>
                                        <a href="#" class="btn btn-primary" role="button">
                                            Button
                                        </a>

                                        <a href="#" class="btn btn-default" role="button">
                                            Button
                                        </a>
                                    </p>

                                </div>
                            </div>
                        }
                    }

The Error i got is :

Severity    Code    Description Project File    Line    Suppression State
Error   CS1579  foreach statement cannot operate on variables of type 'System.Web.Mvc.ActionResult' because 'System.Web.Mvc.ActionResult' does not contain a public definition for 'GetEnumerator'  SmartBookLibrary    C:\Users\Firas\documents\visual studio 2015\Projects\SmartBookLibrary\SmartBookLibrary\Views\Book\Book.cshtml   73  Active

Note : I Tried to change the model type to this :

@model IEnumerable <SmartBookLibrary.Models.Book>

and the error is gone , but i got error for all the view code (any code start with @model) , and this is a sample of error i got:

    Severity    Code    Description Project File    Line    Suppression State
Error   CS1061  'IEnumerable<Book>' does not contain a definition for 'Book_name' and no extension method 'Book_name' accepting a first argument of type 'IEnumerable<Book>' could be found (are you missing a using directive or an assembly reference?)   SmartBookLibrary    C:\Users\Firas\documents\visual studio 2015\Projects\SmartBookLibrary\SmartBookLibrary\Views\Book\Book.cshtml   4   Active

can any one help me with a solution ! and thanks

Upvotes: 0

Views: 2090

Answers (1)

Shyju
Shyju

Reputation: 218722

The error is self explanatory. SameAuthor() action method returns an ActionResult type. You cannot loop through that !

Looks like your SameAuthor action method returns a view (markup) with the data you wanted. So why not just include that in the main view with Html.Action helper method

@Html.Action("SameAuthor", Model.Author_name)

Or you should create a view model for your view which has the details of one book and the a collection of books for "Books from the same author".

public class BooksDetailsVm
{
    public Book Book { set;get;}
    public List<Book> BooksFromSameAuthor { set;get;}
}

and in your GET action, load this data

public ActionResult Book(int? id)
{
    if (id != null)
    {
        var vm = new BooksDetailsVm();
        var b = db.Books.Where(O => O.Book_id == id).Single();
        vm.Book = b;
        var user = db.AspNetUsers.Where(P => P.Id == b.User_ID).Single();
        vm.BooksFromSameAuthor = db.Books.Where(t => t.Author_name == b.Author_name).Take(4).ToList();
        ViewBag.User_ID = user.UserName;
        return View(vm);
    }
    return HttpNotFound();
}

And your view, which is strongly typed to our new BookDetailsVm

@model BookDetailsVm
<h1>@Model.Book.Name</h1>
<h4>Books from same author</h4>
@foreach(var item in Model.BooksFromSameAuthor)
{
    <p>@item.Name</p>
}

Upvotes: 1

Related Questions