PartialView 只获取一个视图的数据

PartialView getting data only for one View

大家好,我正在使用 asp mvc4 构建网站,我在 Views 中遇到问题 首先,我的 _Layout View 为 header 调用了 partialView,而我的部分 View 正在从模型中获取数据以填充 header 中的一些数据, 它只是在主(索引)视图中工作,但移动到另一个视图只是调用部分视图而不从数据库获取数据。 希望我解释得很好 :D

_布局视图

@model MvcApplication1.Models.SchoolInfo
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>@Html.DisplayFor(model => model.SiteName)</title>

<!-- Bootstrap Core CSS -->
<link href="~/Content/Template/css/bootstrap.min.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="~/Content/Template/css/business-casual.css" rel="stylesheet">

<!-- Fonts -->
<link href="http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Josefin+Slab:100,300,400,600,700,100italic,300italic,400italic,600italic,700italic" rel="stylesheet" type="text/css">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<body>
    <header>
        <section id="login">
                    @Html.Partial("_LoginPartial")
            @if (Request.IsAuthenticated)
            {
                    @Html.Partial("_SiteHeaderPartial")                            
                
            }
                </section>
    </header>
    @RenderBody()
    @if (Request.IsAuthenticated){
    <footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <p>Copyright &copy; Your Website footer</p>
            </div>
        </div>
    </div>
</footer>
    }
<!-- jQuery -->
<script src="~/Scripts/Template/js/jquery.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="~/Scripts/Template/js/bootstrap.min.js"></script>

<!-- Script to Activate the Carousel -->
<script>
$('.carousel').carousel({
    interval: 5000 //changes the speed
})
</script>

_SiteHeaderPartial

@model MvcApplication1.Models.SchoolInfo
<div class="brand">@Html.DisplayFor(model => model.SchoolName)</div>
<div class="address-bar">Country | Address : @Html.DisplayFor(model => model.SchoolAddress) | 011 @Html.DisplayFor(model => model.SchoolPhone)

Index and About views are the same (empty).

如果您没有在 About.cshtml 视图中看到详细信息,那是因为您没有将 SchoolInfo 模型传递给该视图(或者模型未填充数据)。但是布局不应该包含模型声明(除非它是一个基本模型,所有使用该布局的视图都从该基本模型派生)。

从布局中删除 @model MvcApplication1.Models.SchoolInfo,而不是 @Html.Partial("_SiteHeaderPartial"),使用

@Html.Action("SiteHeader", "yourControllerName")

其中 SiteHeader() 是获取数据的控制器方法,returns 是局部视图,例如

[ChildOnlyAction]
public PartialViewResult SiteHeader()
{
    SchoolInfo model = ... // populate your model
    return PartialView("_SiteHeaderPartial", model);
}

Sife 注意:您还需要删除 <title>@Html.DisplayFor(model => model.SiteName)</title> 或将其替换为(比如)<title>@ViewBag.Title</title> 并在每个视图中使用 @{ ViewVag.Title = someValue; }[=20 将值传递给部分=]