【发布时间】:2013-06-12 14:38:24
【问题描述】:
部分视图不返回任何数据。当我在 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());
}
}
Page.cshtml
@Html.Action("Module")
_ContentBlock.cshtml
@model IEnumerable<Models.ContentBlock>
@foreach (var item in Model)
{
@Html.DisplayFor(n => item.Content)
}
【问题讨论】:
-
您使用什么调试工具检查
_ContentBlock.cshtml部分中的模型是否为空?还有这个动作怎么称呼?你在使用一些 AJAX 调用吗?你能展示你的代码吗? -
达林,我已经更新了我的问题。是的,_ContentBlock.cshtml 中的模型为空。我没有使用 AJAX。
-
但是您如何调用问题中显示的控制器操作?正是这个控制器动作将模型传递给局部。
-
达林,我知道,我必须使用 Html.Action() 方法调用局部视图。但是如果您检查我的代码(上图),实际上我在 Partial View.Offfff 中调用局部视图:D I有困惑:S
-
所以你用
Html.Partial而不是Html.Action?
标签: asp.net-mvc razor model actionresult asp.net-mvc-partialview