user3147373
user3147373

Reputation: 23

How to call a controller method inside the view?

My problem is when i'm trying to get the best 4 posts who's match the same author name , as you see in the following image :

this is the controller :

Controller Code

and here is the view :

View code

so how i can call the controller method from the view , what's the code will looks like ? and thanks ..

also here is the controller method :

public ActionResult SameAuthor(string author)
    {

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

and the view code (book page , the following code is a part of book page , that show the book details and info , so this code for show under the post details the most 4 post for same author) is :

 //DISPLAYING 4 BOOKS FOR SAME AUTHOR   
<div class="well">
    <div class="row">
        @foreach (var item in Model)
        {
            <div class="col-sm-6 col-md-3">
                <div class="thumbnail">
                    <img src="/Books/RetrieveImage/@item.Book_id" alt="Generic placeholder thumbnail">
                </div>

                <div class="caption">
                    <h3>@item.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>

and thanks

Upvotes: 0

Views: 604

Answers (1)

Shyju
Shyju

Reputation: 218702

If you are trying to show the result of your SameAuthor action method inside another view, you may use Html.Action helper method to do so.

So in your some other razor view, you can include the below line

@Html.Action("SameAuthor","Book",new {author="Author name here"})

Upvotes: 2

Related Questions