-1

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)

enter image description here

enter image description here

Index and About views are the same (empty).

Community
  • 1
  • 1
yazan
  • 518
  • 3
  • 16
  • Debug your code and check whether your database is hit second time or not. – Rajput Dec 17 '16 at 12:04
  • its only getting data in one view the index one,going to another one its calling everything but data don't shows up, going back to the index view will get the data – yazan Dec 17 '16 at 12:07
  • paste the screenshot of both pages index and others – Rajput Dec 17 '16 at 12:10
  • `@Html.Partial()` does not call a server method to get data. For that, you need `@Html.Action()`. But a layout should never include a declaration for a model unless its a base model form which all other views using that layout derive –  Dec 17 '16 at 12:15
  • @StephenMuecke Sir but how he is getting data for the first time of rendering layout view. He is also telling his database is hit for second time also on another page. – Rajput Dec 17 '16 at 12:36
  • the 2nd picture country and address and 025 they are just text in the view – yazan Dec 17 '16 at 12:39
  • @Rajput, Because OP is passing a model to the view in the first (`Index()` method, but not passing the model in the 2nd (`About()`) method –  Dec 17 '16 at 12:39
  • first pic School name and something and the phone number is from db – yazan Dec 17 '16 at 12:40
  • @yazan, What your doing here is awful practice. Remove `@model ...` from your partial, and replace `@Html.Partial()` with `@Html.Action()` to call a server method that gets your data and returns a partial view. –  Dec 17 '16 at 12:41
  • You mean i must use an action method and use my model in the controller and return PartialView for that model – yazan Dec 17 '16 at 12:43
  • i tried this but couldn't of and error maybe because of @Model as you just said – yazan Dec 17 '16 at 12:44
  • 1
    You also need to remove `@Html.DisplayFor(model => model.SiteName)` and anywhere else you refer to `@Model` in the layout file –  Dec 17 '16 at 12:47
  • @StephenMuecke Thanks alot stephen if you would post a solution to accept it ,Thanks again. – yazan Dec 17 '16 at 13:29

1 Answers1

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

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