部分视图不返回任何数据。当我在 PartialView 页面(_ContentBlock.cshtml)上使用调试工具检查时,模型似乎为空。
控制器
public ActionResult Module()
{
int RouteDataValue = default(int);
if (RouteData.Values["id"] != null)
{
RouteDataValue = int.Parse(RouteData.Values["id"].ToString());
}
using (Models.ContentModel db = new Models.ContentModel())
{
var Query = from n in db.PageModule
join m in db.Module on n.ModuleId equals m.ModuleId
where n.PageId == RouteDataValue
select m.PhysicalPath;
return PartialView(Query.Single()); //returns PartialView such as ~/Modules/Content/_ContentBlock.cshtml
}
}
public PartialViewResult ContentBlock()
{
using (Models.ContentModel db = new Models.ContentModel())
{
return PartialView("~/Modules/Content/_ContentBlock.cshtml", db.ContentBlock.Where(n => n.PageId == 2).Single());
}
}
页面.cshtml
@Html.Action("Module")
_ContentBlock.cshtml
@model IEnumerable<Models.ContentBlock>
@foreach (var item in Model)
{
@Html.DisplayFor(n => item.Content)
}