PartialView仅为一个View获取数据

时间:2021-11-24 03:35:55

Hey guys i'm using asp mvc4 for building a site,im having trouble in Views First of all i have my _Layout View calling a partialView for header and my partial View is getting data from a model in to fill some data in the header, It's working by the way just in the Main (index) view but moving to another views just call the Partial view without getting data from database. Hope i explained Well :D

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

_layout View

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

PartialView仅为一个View获取数据

PartialView仅为一个View获取数据

Index and About views are the same (empty).

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

1 个解决方案

#1


1  

If your not seeing the details in the About.cshtml view, its because you have not passed a SchoolInfo model to that view (or the model is not populated with the data). However a layout should not include a model declaration (unless its a base model from which all views that use that layout derive).

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

Remove @model MvcApplication1.Models.SchoolInfo from the layout and instead of @Html.Partial("_SiteHeaderPartial"), use

从布局中删除@model MvcApplication1.Models.SchoolInfo,而不是@ Html.Partial(“_ SiteHeaderPartial”),使用

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

where SiteHeader() is a a controller method that gets your data and returns a partial view, for example

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

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

Sife note: You also need to remove <title>@Html.DisplayFor(model => model.SiteName)</title> or replace it with (say) <title>@ViewBag.Title</title> and in each view, pass the value to the partial using @{ ViewVag.Title = someValue; }

Sife note:您还需要删除

@ Html.DisplayFor(model => model.SiteName)</ title>或将其替换为(例如) @ ViewBag.Title </ title>并在每个视图中,使用@ {ViewVag.Title = someValue将值传递给partial; }</p>

#1


1  

If your not seeing the details in the About.cshtml view, its because you have not passed a SchoolInfo model to that view (or the model is not populated with the data). However a layout should not include a model declaration (unless its a base model from which all views that use that layout derive).

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

Remove @model MvcApplication1.Models.SchoolInfo from the layout and instead of @Html.Partial("_SiteHeaderPartial"), use

从布局中删除@model MvcApplication1.Models.SchoolInfo,而不是@ Html.Partial(“_ SiteHeaderPartial”),使用

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

where SiteHeader() is a a controller method that gets your data and returns a partial view, for example

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

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

Sife note: You also need to remove <title>@Html.DisplayFor(model => model.SiteName)</title> or replace it with (say) <title>@ViewBag.Title</title> and in each view, pass the value to the partial using @{ ViewVag.Title = someValue; }

Sife note:您还需要删除

@ Html.DisplayFor(model => model.SiteName)</ title>或将其替换为(例如) @ ViewBag.Title </ title>并在每个视图中,使用@ {ViewVag.Title = someValue将值传递给partial; }</p>