I have a HomeController which has a Navigation ActionResult to perform a query to get the top level elements:
public ActionResult Navigation()
{
var navigationModel = (from m in db.Navigations where (m.Main == true) orderby m.Position select m);
return View(navigationModel);
}
I then have a Navigation View which outputs the results like so:
@model IEnumerable<WebApplication1.Models.Navigation>
<ul class="nav sf-menu clearfix">
@foreach (var item in Model)
{
@Html.MenuLink(item.Title, item.Action, item.Controller)
}
</ul>
I would like so add a second navigation layer so contemplating something like:
public ActionResult Navigation()
{
var navigationModel = (from m in db.Navigations where (m.Main == true) orderby m.Position select m);
var contentModel = (from n in db.Contents where (n.NavigationId = navigationModel.Id) orderby n.Position select n);
return View(navigationModel, contentModel);
}
I get the error Cannot convert method group 'Id' to non-delegate type 'int?'. Did you intend to invoke the method?
I would like also like to pass the second query to the View, something like:
@model IEnumerable<WebApplication1.Models.Navigation>
@model IEnumerable<WebApplication1.Models.Contents>
<ul class="nav sf-menu clearfix">
@foreach (var item in Model)
{
@Html.MenuLink(item.Title, item.Action, item.Controller)
<ul>
@foreach (var item in Model)
{
@Html.MenuLink(item.Title, "Article", "Home")
}
</ul>
}
</ul>
I realise my use of models may be completely inaccurate, just trying to emphasise what data where.
Any help would be much appreciated :-)
Here are the Navigation and Content classes to assist with any solutions:
Navigation.cs
public partial class Navigation
{
public int Id { get; set; }
public string Title { get; set; }
public Nullable<int> Position { get; set; }
public bool Main { get; set; }
public string Action { get; set; }
public string Controller { get; set; }
}
Content.cs
public partial class Content
{
public int Id { get; set; }
public Nullable<int> NavigationId { get; set; }
public string Title { get; set; }
public string Content1 { get; set; }
public Nullable<int> Position { get; set; }
public string Image { get; set; }
public string Sub { get; set; }
public Nullable<bool> Active { get; set; }
public string Url { get; set; }
public string Summary { get; set; }
}
Despite the helpful advise I was still left with the error Operator '==' cannot be applied to operands of type 'int?' and 'method group'
so tried an alternative method, which is to use a join query, so I have modified the MVC structures as follows:
NavigationViewModel
public class NavigationViewModel
{
public int NavigationId { get; set; }
public string NavigationTitle { get; set; }
public string NavigationAction { get; set; }
public string NavigationController { get; set; }
public int ContentId { get; set; }
public string ContentTitle { get; set; }
}
HomeController
public ActionResult Navigation()
{
var navigationModel = from c in db.Contents
join n in db.Navigations on c.NavigationId equals n.Id
where (n.Main == true && c.Active == true)
orderby n.Position
select new NavigationViewModel()
{
NavigationId= n.Id,
NavigationTitle = n.Title,
NavigationAction = n.Action,
NavigationController = n.Controller,
ContentId = c.Id,
ContentTitle = c.Title
};
return View(navigationModel);
}
Navigation
@model IEnumerable<WebApplication1.Models.NavigationViewModel>
@foreach (var item in Model)
{
@item.NavigationId @item.NavigationTitle @item.NavigationAction @item.NavigationController @item.ContentId @item.ContentTitle
}
So this gets the data, but I have a similar issue to what I started with, how do I now step through the data so I have something like this:
<ul class="nav sf-menu clearfix">
// Loop through items
// Are there multiple intances of the NavigationId?
// No
<li>navigation link here</li>
// Yes
<li class="sub-menu">navigation link here
<ul>
// Loop through all content links
<li>content link here</li>
// End loop
</ul>
</li>
// End Loop
</ul>