29

I am starting with MVC5 and created first project from MVC5 Getting Started.

Now trying with Partial Rendering and added a method in MoviesController as below

[ChildActionOnly]
public ActionResult PriceRange()
{
   var maxprice = db.Movies.Max(m => m.Price);
   var minprice = db.Movies.Min(m => m.Price);
   ViewBag.MaxPrice = maxprice;
   ViewBag.MinPrice = minprice;
   return PartialView();
}

It sets Min and Max price from Movies collection into ViewBag that are later displayed at view. I am trying to render it on different views.

First i tried to render it at Views/Movies/Index.cshtml as below

@{Html.RenderAction("PriceRange");}

It works well there and results displayed correctly because it is using MoviesController, the same class where method PriceRange defined.

Then i tried to render it at Views/Hello/Index.cshtml (this view is using HelloWorldController) with following code (first passing Action name then Controller name)

@{Html.RenderAction("PriceRange", "MoviesController");}

Here it is giving run-time error

The controller for path '/HelloWorld/Index' was not found or does not implement IController.

Here is complete code from Views/Hello/Index.cshtml

@{
    ViewBag.Title = "Movie List";
}
<h2>My Movie List</h2>
<p>Hello from our view template</p>
@{Html.RenderAction("PriceRange", "MoviesController");}

I found few examples through Google, they are calling RenderAction helper the same way, first passing Action name then Controller name.

I couldn't understand what the wrong i am doing here. Can someone point out?

0

2 Answers 2

59

It might be that you're adding the "Controller" postfix to the controller name which isn't required.

Try:

@{Html.RenderAction("PriceRange", "Movies");}
Sign up to request clarification or add additional context in comments.

2 Comments

@Html.Action does this without the curly braces.
@Html.Action does without braces but most of the time you need RenderAction due to better performance.
1

The controller name needs to be "Movies" and not "MoviesController". Because now I believe it is looking for a controller called "MoviesControllerController".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.