PartialView仅为一个View获取数据

时间:2016-12-17 11:53:12

标签: asp.net-mvc-4 asp.net-mvc-partialview

嘿伙计们我使用asp mvc4来构建一个网站,我在Views中遇到了麻烦 首先,我让我的_Layout View调用了一个partialView for header,我的部分View正在从模型中获取数据以填充标题中的一些数据, 它只是在Main(索引)视图中工作,但移动到另一个视图只需调用Partial视图而不从数据库获取数据。 希望我解释得好:D

_layout查看

@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)

enter image description here

enter image description here

  

索引和关于视图是相同的(空)。

1 个答案:

答案 0 :(得分:1)

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

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

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

其中SiteHeader()是一个获取数据并返回局部视图的控制器方法,例如

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

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