Reputation: 119
I have a list of categories in the Sidebar.
@foreach (var item in Model) {
<li><a href="~/Books/Category/@item.Title">@item.Title</a></li>
}
And I want to display the products of this category by clicking on the category. To do this, I implemented the method ViewCategory.
public ActionResult ViewCategory(string name) { ... }
But I do not know how to pass the parameter correctly. I'm trying to write something like that, but I understand that doing something wrong ...
@Html.Action("ViewCategory", "Books", new {Title=item.Title})
Help me please
UPDATE
I have a View Index, and a method in which I bring up a list of my products
public ActionResult Index()
{
HttpResponseMessage response = WebApiClient.GetAsync("Books").Result;
var booksList = response.Content.ReadAsAsync<IEnumerable<BookDto>>().Result;
return View(booksList);
}
I need to display only products that belong to this category when choosing a category. I list the categories with PartialView
<ul>
@foreach (var item in Model) {
@*<li><a href="~/Books/@item.Title"></a></li>*@
@Html.Action("ViewCategory", "Books", new { name = item.Title })
}
To do this, I wrote a method that I try to use instead of
public ActionResult ViewCategory(string name)
{
HttpResponseMessage responseBooks = WebApiClient.GetAsync("Books").Result;
List<BookDto> booksList = responseBooks.Content.ReadAsAsync<IEnumerable<BookDto>>().Result.ToList();
for (int i = 0; i < booksList.Count; i++)
{
if (booksList[i].CategoryName != name)
{
booksList.Remove(booksList[i]);
}
}
return View("Category");
}
But now I have NullReferenceException...
Upvotes: 0
Views: 980
Reputation: 122
You can try using
@Html.Action("Controller","Name", new { name = item.Title })
Upvotes: 1
Reputation: 381
You can use it as following.
@{ Html.RenderAction("ViewCategory", "Books",
new {param1 = "value1", param2 = "value2" }); }
Upvotes: 1
Reputation: 422
Just change
@Html.Action("ViewCategory", "Books", new {Title=item.Title})
to
@Html.Action("ViewCategory", "Books", new {name = item.Title})
Upvotes: 2