部分视图中的模型为空

cha*_*ara 4 asp.net-mvc razor asp.net-mvc-4

我正在尝试在布局页面中添加局部视图.

模型

public class SummaryPanelModel
    {
        public int TotalDesignDocs { get; set; }
        public int TotalVendorDocs { get; set; }
        public int TotalBusinessDocs { get; set; }
        public int TotalManagementDocs { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

SummaryPanel_Partial局部视图控制器:

 public ActionResult SummaryPanel_Partial()
        {
            rep = new SummaryRepository();
            SummaryPanelModel model = new SummaryPanelModel();
            model = rep.ReadsummaryPanel();//read from database
            return View(model);
        }
Run Code Online (Sandbox Code Playgroud)

布局页面

<!DOCTYPE html>
<html lang="en">
@{
    Layout = null;
}

 @Html.Partial("SummaryPanel_Partial")
Run Code Online (Sandbox Code Playgroud)

SummaryPanel_Partial局部视图:

@model Doc.Web.Models.SummaryPanel.SummaryPanelModel

<div id="pnlBar">
    @Html.Label(Model.TotalDesignDocs.ToString())
<div/>
Run Code Online (Sandbox Code Playgroud)

尽管我已经在控制器动作中传递了模型,但模型在局部视图中始终为null.

Row*_*man 6

@Html.Partial("SummaryPanel_Partial")
Run Code Online (Sandbox Code Playgroud)

以这种方式调用部分不会调用控制器+动作.相反,它只是查找视图SummaryPanel_Partial并呈现它.由于此时未提供模型,因此模型为空.

相反,调用Html.Action,它将调用控制器+动作.

@Html.Action("SummaryPanel_Partial", "Controller")
Run Code Online (Sandbox Code Playgroud)

并改变你的行动:

public ActionResult SummaryPanel_Partial()
{
    // ...
    return PartialView(model);
}
Run Code Online (Sandbox Code Playgroud)