网站首页的栏目名称,图片轮播,新闻内容,公司简介等均从数据库中读取
View部分:
@using Droplets.Models; @{ ViewBag.Title = "Droplets"; List<WenZhang> lunbo = ViewBag.LunBo; List<WenZhang> gundong = ViewBag.GuoDong; WenZhang jianjie = ViewBag.JianJie; WenZhang world = ViewBag.WorldWide; List<WenZhang> news = ViewBag.News; WenZhang zhaopin = ViewBag.ZhaoPin; } <link href="~/Content/css/normalize.css" rel="stylesheet" /> <link href="~/Content/css/default.css" rel="stylesheet" /> <link href="~/Content/css/pgwslideshow.css" rel="stylesheet" /> <div class="content"> <div class="hidden"></div> <section class="start"> <div class="buehne-cont"> <div class="buehne keyvisual"> @if (lunbo != null) { foreach (WenZhang item in lunbo) { <div class="keyvisual-element" style="position:relative;"> <a href="" target="_self"> <img src='@item.PictureCaptions' alt="@item.Title" /> </a> </div> } } </div> <div class="pag-container cf"> <div class="pagination cf" style="display: block;"> </div> </div> </div> <section class="buehne-info"> <div class="c21 cf inner"> <div class="s1"> @MvcHtmlString.Create(jianjie.Content) </div> <div class="s2"> <h3>Locations worldwide</h3> @*@MvcHtmlString.Create(world.Content)*@ <a href="/Content/Index?id=@world.Id" title="Rheinmetall Defence "> <img class="hover" src="/Content/background_1/隐地图.png" alt="Locations worldwide" onmouseover="this.src='/Content/background_1/显地图.png';"onmouseout="this.src = '/Content/background_1/隐地图.png';"> </a> </div> </div> </section> <section class="weiss"> <div class="inner cf"> <div class="c21 cf fixh"> <div class="s1"> <div class="htmleaf-container"> <div class="htmleaf-content bgcolor-3"> <ul class="pgwSlideshow"> @if (gundong != null) { foreach (WenZhang item in gundong) { if (item.PictureCaptions != null) { <li> <a href="/Product/Index?id=@item.Id" target="_blank"> <img src="@item.PictureCaptions" alt="@item.Title"> </a> </li> } } } </ul> </div> </div> <script src="~/Content/js/jquery-1.11.0.min.js"></script> <script src="~/Content/js/pgwslideshow.js"></script> <script type="text/javascript"> $(document).ready(function () { $('.pgwSlideshow').pgwSlideshow({ transitionEffect: 'fading', autoSlide: true }); }); </script> </div> <div class="s2"> <div class="s2"> <div class="box teaser withtopborder bottomBorder"> <h3 class="tpt"> <a href="" title="News" class="none"> News </a> </h3> @if (news != null) { foreach (WenZhang item in news) { <p style="border-bottom: 1px solid #efefef;"> <span class="date"> @item.CreateTime.ToShortDateString() </span> <a class="contentlink news" href="/Content/Index?id=@item.Id" title=""> @item.Title </a> </p> } } </div> <div class="box teaser withtopborder bottomBorder"> <h3 class="tpt"> <a href="/Content/Index?id=@zhaopin.Id" title="Job vacancies at Droplets" target=""> @MvcHtmlString.Create(zhaopin.Title) </a> </h3> <a class="picturelink" href="/Content/Index?id=@zhaopin.Id" title="Job vacancies at Droplets" target=""> <img alt="" src="@zhaopin.PictureCaptions"> </a> <a href="" title="Job vacancies at Droplets" target=""> </a> </div> </div> </div> </div> </div> </section> </section> </div>
Controller部分:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Data.Entity; using Droplets.Models; namespace Droplets.Controllers { public class HomeController :AppController//调用AppController { // // GET: /Home/ Droplets.Models.DropletsEntities db; public HomeController() { db = new DropletsEntities(); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); }//可以让下面方法直接调用“db” public ActionResult Index() { //例如图片新闻来自于栏目19 var q = from t in db.WenZhang where t.LanMu2_Id == 19 orderby t.Id descending select t ; var list = q.ToList(); ViewBag.LunBo = list; //图片轮播来源于栏目20 var q1 = from t in db.WenZhang where t.LanMu2_Id == 1 orderby t.Id descending select t; list = q1.ToList(); ViewBag.GuoDong = list; //公司简介来源于文章101 var q2 = from t in db.WenZhang where t.Id == 17 select t; var wenzhan = q2.FirstOrDefault(); ViewBag.JianJie = wenzhan; //地图信息 var q3 = from t in db.WenZhang where t.Id == 37 select t; var wenzhang = q3.FirstOrDefault(); ViewBag.WorldWide = wenzhang; //新闻信息 var q4 = from t in db.WenZhang where t.LanMu2_Id == 3 orderby t.Id descending select t; var news = q4.Take(5); list = news.ToList(); ViewBag.News = list; //主页招聘信息模块Id49 var q5 = from t in db.WenZhang where t.Id == 49 select t; var zhaopin = q5.FirstOrDefault(); ViewBag.ZhaoPin = zhaopin; return View(); } //public ActionResult Detail(int Id) //{ // WenZhang wenzhang = db.Get(Id); // wenzhang.BrowseCount += 1; // db.update(wenzhang); // ViewBag.Notice = GetNotice(); // return View(wenzhang); //} } } Layout部分需要时自行添加,这里只是读取其中的一级栏目 <div class="main-nav-container"> <nav class="main-nav mdd inner noindex"> <ul class="cf level-1 noindex"> @if (lanmu1 != null) { var i = 1; foreach (LanMu1 item1 in lanmu1) { if (i == 1) { <li class="drop"> <a href="/Home/Index" class="droplink mo">@item1.Name</a> </li> } else if (i == 2) { <li class="drop"> <a href="/Product/Index?id=0" class="droplink mo">@item1.Name</a> </li> } else if (i == 3) { <li class="drop"> <a href="/Content/Index?id=6" class="droplink mo">@item1.Name</a> </li> } else if (i == 4) { <li class="drop"> <a href="/Content/Index?id=3" class="droplink mo">@item1.Name</a> </li> } else if (i == 5) { <li class="drop"> <a href="/Content/Index?id=7" class="droplink mo">@item1.Name</a> </li> } else { <li class="drop"> <a href="" class="droplink mo">@item1.Name</a> </li> } i++; } } </ul> </nav> </div> AppController: public class AppController : Controller { //基础控制器,控制布局页 //GET: /App/ private DropletsEntities db = new DropletsEntities(); public DropletsEntities Datacontext { get { return db; } } //public string GetLanMu(string lanmuname) //{ // TempData["lanmuname"] = lanmuname; // return lanmuname; //} public AppController() { var n1 = from t1 in db.LanMu1 select t1; var list = n1.ToList(); ViewBag.LanMu1Name = list; } }