我正在尝试在布局页面中添加局部视图。
模型
public class SummaryPanelModel
{
public int TotalDesignDocs { get; set; }
public int TotalVendorDocs { get; set; }
public int TotalBusinessDocs { get; set; }
public int TotalManagementDocs { get; set; }
}
SummaryPanel_Partial部分视图控制器:
public ActionResult SummaryPanel_Partial()
{
rep = new SummaryRepository();
SummaryPanelModel model = new SummaryPanelModel();
model = rep.ReadsummaryPanel();//read from database
return View(model);
}
布局页面
<!DOCTYPE html>
<html lang="en">
@{
Layout = null;
}
@Html.Partial("SummaryPanel_Partial")
SummaryPanel_Partial Partial View:
@model Doc.Web.Models.SummaryPanel.SummaryPanelModel
<div id="pnlBar">
@Html.Label(Model.TotalDesignDocs.ToString())
<div/>
尽管我已经在控制器动作中传递了模型,但在局部视图中模型始终为null。
答案 0 :(得分:6)
@Html.Partial("SummaryPanel_Partial")
以这种方式调用部分不会调用控制器+操作。相反,它只是查找视图SummaryPanel_Partial
并呈现它。由于此时未提供模型,因此模型为空。
相反,请致电Html.Action
,这将调用控制器+操作。
@Html.Action("SummaryPanel_Partial", "Controller")
改变你的行动:
public ActionResult SummaryPanel_Partial()
{
// ...
return PartialView(model);
}
答案 1 :(得分:1)
尝试使用PartialViewResult
public PartialViewResult SummaryPanel_Partial()
{
rep = new SummaryRepository();
SummaryPanelModel model = new SummaryPanelModel();
model = rep.ReadsummaryPanel();//read from database
return PartialView(model);
}