Nav header 1
nav 1 sub item 1
nav 1 sub item 2
Nav header 2
nav 2 sub item 1
nav 2 sub item 2
I would like to use one model to return all sub items, then use a parameter to return the correct sub items based on the header item.
@foreach (var subItem in Model.nav)
{
<li>this should show sub items for header 1</li>
}
@foreach (var subItem in Model.nav)
{
<li>and this should show sub items for header 2</li>
}
How I use the id of the header item to determine which sub items to get. I can hard code the header id in the view, but I don't know how to pass that to the controller.
That really depends on your model, but assuming your model contains the id then you can include a where statemetn in your foreach loop.
For example:
@foreach (var subItem in Model.nav.Where(n=>n.Id == 1).ToList())
{
<li>this should show sub items for header 1</li>
}
@foreach (var subItem in Model.nav.Where(n=>n.Id == 2).ToList())
{
<li>and this should show sub items for header 2</li>
}
Well I agree that it breaks the no logic in the view rule, but depending on the size of the web applicationg it could be ok.
Ideally would create a view model with the data you need and in the view model have a collection of Nav headers. The view would then just iterate through the NavHeaders collection which in turn has the data for the header and a property for all the sub menu
items.
You can take this one step further by creating an html helper to render the view for you, in this case the logic is interely in a class and not in the view.
jypelton
Member
153 Points
154 Posts
Passing an id to the contoller from the view
May 01, 2012 02:26 PM|LINK
I have a nav typical hierarchical nav like this:
Nav header 1
nav 1 sub item 1
nav 1 sub item 2
Nav header 2
nav 2 sub item 1
nav 2 sub item 2
I would like to use one model to return all sub items, then use a parameter to return the correct sub items based on the header item.
@foreach (var subItem in Model.nav)
{
<li>this should show sub items for header 1</li>
}
@foreach (var subItem in Model.nav)
{
<li>and this should show sub items for header 2</li>
}
How I use the id of the header item to determine which sub items to get. I can hard code the header id in the view, but I don't know how to pass that to the controller.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Passing an id to the contoller from the view
May 01, 2012 04:02 PM|LINK
That really depends on your model, but assuming your model contains the id then you can include a where statemetn in your foreach loop.
For example:
@foreach (var subItem in Model.nav.Where(n=>n.Id == 1).ToList()) { <li>this should show sub items for header 1</li> } @foreach (var subItem in Model.nav.Where(n=>n.Id == 2).ToList()) { <li>and this should show sub items for header 2</li> }Blog | Twitter : @Hattan
jypelton
Member
153 Points
154 Posts
Re: Passing an id to the contoller from the view
May 01, 2012 05:38 PM|LINK
Ok. It seems to break the "no logic in the view" rule, but it works for me. Thanks again.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Passing an id to the contoller from the view
May 01, 2012 05:50 PM|LINK
Well I agree that it breaks the no logic in the view rule, but depending on the size of the web applicationg it could be ok.
Ideally would create a view model with the data you need and in the view model have a collection of Nav headers. The view would then just iterate through the NavHeaders collection which in turn has the data for the header and a property for all the sub menu items.
You can take this one step further by creating an html helper to render the view for you, in this case the logic is interely in a class and not in the view.
Blog | Twitter : @Hattan