当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc 4 - PartialView getting data only for one View

问题描述:

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

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

Index and About views are the same (empty).

网友答案:

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

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

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

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

[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; }

分享给朋友:
您可能感兴趣的文章:
随机阅读: