我有一个HomeController,它有一个Navigation ActionResult来执行查询以获得顶级元素:
public ActionResult Navigation()
{
var navigationModel = (from m in db.Navigations where (m.Main == true) orderby m.Position select m);
return View(navigationModel);
}
然后我有一个导航视图,输出结果如下:
@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>
我想添加第二个导航层,以便考虑如下:
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);
}
我收到错误Cannot convert method group 'Id' to non-delegate type 'int?'. Did you intend to invoke the method?
我还想将第二个查询传递给View,例如:
@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>
我意识到我对模型的使用可能完全不准确,只是想强调哪些数据在哪里。
非常感谢任何帮助: - )
以下是帮助解决方案的导航和内容类:
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; }
}
尽管有用的建议我仍然留下错误Operator '==' cannot be applied to operands of type 'int?' and 'method group'
所以尝试了另一种方法,即使用连接查询,所以我修改了MVC结构如下:
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);
}
导航
@model IEnumerable<WebApplication1.Models.NavigationViewModel>
@foreach (var item in Model)
{
@item.NavigationId @item.NavigationTitle @item.NavigationAction @item.NavigationController @item.ContentId @item.ContentTitle
}
所以这得到了数据,但是我遇到了类似的问题,我现在如何逐步完成数据,所以我有这样的事情:
<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>
答案 0 :(得分:1)
您可以使用视图模型:
public class NavigationViewModel
{
public IEnumerable<Navigation> Navigations { get; set; }
public IEnumerable<Content> Contents { get; set; }
}
您的视图将采用此视图模型:
@model YourNamespace.ViewModels.NavigationViewModel
您的控制器将初始化此视图模型并将其传递给视图:
public ActionResult Navigation() {
var navigations = your query...
var contents = your other query...
return View(new NavigationViewModel {
Navigations = navigations,
Contents = contents
});
}
您的观点可以访问Model.Navigations
和Model.Contents
。
答案 1 :(得分:0)
尝试使用==而不是=来编译错误(除非你真的打算调用我怀疑的代理)。
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.HasValue &&(n.NavigationId.value == navigationModel.Id)) orderby n.Position select n);
return View(new Tuple<Navigation, Contents> (navigationModel, contentModel));
}
如果您有施法并发症,则可能必须使用toString()等转换。
对于第二个问题,如果您不想创建新课程,请使用元组(http://www.dotnetperls.com/tuple)。
@model Tuple<WebApplication1.Models.Navigation, WebApplication1.Models.Contents>
<ul class="nav sf-menu clearfix">
@foreach (var item in Model.Item1)
{
@Html.MenuLink(item.Title, item.Action, item.Controller)
<ul>
@foreach (var item in Model.Item2)
{
@Html.MenuLink(item.Title, "Article", "Home")
}
</ul>
}
</ul>