MVC4制作網(wǎng)站教程第四章 前臺欄目瀏覽4.5
三、欄目
3.4前臺欄目瀏覽
網(wǎng)站的前臺頁面,頂部要能顯示根欄目,點擊欄目名稱進入欄目中要子欄目導(dǎo)航,欄目頁中還必須有當(dāng)前路徑。先做這三部分
1)、根欄目
打開【CategoryController】,添加[PartialRoot]Action
/// <summary>
/// 根欄目
/// </summary>
/// <returns></returns>
public ActionResult PartialRoot()
{
return View(categoryRsy.Root());
}
點擊右鍵添加視圖模型類選Category,支架模板選List,勾上創(chuàng)建分部視圖,確定。
除頂部@model IEnumerable<Ninesky.Models.Category>外刪除其他代碼,自己手動寫代碼如下:
@model IEnumerable<Ninesky.Models.Category>
@Html.ActionLink("網(wǎng)站首頁", "Index", "Home")@foreach (var item in Model)
{
@Html.ActionLink(item.Name, "Index", "Category", new { id = item.CategoryId }, null)
}
2)、子欄目導(dǎo)航
在【CategoryController】中添加[PartialChildren(int id)]Action
/// <summary>
/// 子欄目
/// </summary>
/// <param name="id">欄目id</param>
/// <returns></returns>
public ActionResult PartialChildren(int id)
{
return View(categoryRsy.Children(id));
}
右鍵添加分部視圖
@model IEnumerable<Ninesky.Models.Category>
<ul>
@foreach (var item in Model)
{
<li>@Html.ActionLink(item.Name, "Index", "Category", new { id = item.CategoryId }, null)</li>
}
</ul>
3)、路徑
在【CategoryController】中添加[PartialPath(int id)]Action
/// <summary>
/// 欄目路徑
/// </summary>
/// <param name="id">當(dāng)前欄目Id</param>
/// <returns></returns>
public ActionResult PartialPath(int id)
{
List<Category> _path = new List<Category>();
var _category = categoryRsy.Find(id);
while (_category != null)
{
_path.Insert(0, _category);
_category = categoryRsy.Find(_category.ParentId);
}
return View(_path);
}
右鍵添加分部視圖
@model IEnumerable<Ninesky.Models.Category>
您現(xiàn)在的位置:@Html.ActionLink("網(wǎng)站首頁", "Index", "Home")@foreach (var item in Model)
{
@Html.Raw(">>") @Html.ActionLink(item.Name, "Index", "Category", new { id = item.CategoryId }, null)
}
馬上可以看到效果了
打開Layout\_Layout.cshtml布局頁,在頂部導(dǎo)航位置<div class="nav"></div>中添加寫上@Html.Action("PartialRoot","Category")
打開http://localhost:52270/Category/ManageAdd,添加一下幾個欄目。

運行一下看看網(wǎng)站首頁

有效果了!
開始做Index索引頁
在【CategoryController】中添加[Index(int id)]Action
如果欄目Type=2則跳轉(zhuǎn)到Navigation,否則返回CategoryView視圖。
/// <summary>
/// 索引
/// </summary>
/// <param name="id">欄目id</param>
/// <returns></returns>
public ActionResult Index(int id)
{
var _category = categoryRsy.Find(id);
if (_category == null)
{
Error _e = new Error { Title = "錯誤", Details = "指定的欄目不存在", Cause = "你訪問的欄目已經(jīng)刪除", Solution = Server.UrlEncode("<li>返回<a href='" + Url.Action("Index", "Home") + "'>網(wǎng)站首頁</a></li>") };
return RedirectToAction("Error", "Prompt", _e);
}
if (_category.Type == 2) return Redirect(_category.Navigation);
return View(_category.CategoryView,_category);
}
添加強類型視圖

@model Ninesky.Models.Category
@{
ViewBag.Title = "欄目默認(rèn)頁";
Layout = "~/Views/Layout/_Layout.cshtml";
}
<div class="banner">
<img src="~/Skins/Default/Images/banner.jpg" />
</div>
<div class="left">
<div class="children">
<dl>
<dt>@Model.Name</dt>
<dd>@Html.Action("PartialChildren", "Category", new { id = Model.CategoryId })</dd>
</dl>
</div>
</div>
<div class="content_cnt">
<div class="path">@Html.Action("PartialPath", "Category", new { id = Model.CategoryId })</div>
</div>
這個就是欄目的默認(rèn)頁面了。
復(fù)制一份Index.cshtml命名為IndexSingle.cshtml作為單頁欄目的視圖
再復(fù)制一份Index.cshtml命名為IndexAbout.cshtml作為關(guān)于我們欄目的專用視圖,并修改相應(yīng)代碼
@model Ninesky.Models.Category
@{
ViewBag.Title = "關(guān)于我們";
Layout = "~/Views/Layout/_Layout.cshtml";
}
<div class="banner">
<img src="~/Skins/Default/Images/banner.jpg" />
</div>
<div class="left">
<div class="children">
<dl>
<dt>@Model.Name</dt>
<dd>@Html.Action("PartialChildren", "Category", new { id = Model.CategoryId })</dd>
</dl>
</div>
</div>
<div class="content_cnt">
<div class="path">@Html.Action("PartialPath", "Category", new { id = Model.CategoryId })</div>
<div class="singlepage">
<div class="title">@Model.Name About </div>
<p>
<b>NineSky</b>® 是洞庭夕照學(xué)習(xí)Mvc的一個項目。是想通過完成一個網(wǎng)站來不斷的督促自己、不斷的學(xué)習(xí)和實踐。最終希望可以寫出一個可簡潔、易用的網(wǎng)站。
</p>
<p>目的:學(xué)習(xí)mvc4</p>
<p>目標(biāo):簡單、易用、實用</p>
</div>
</div>
打開“關(guān)于我們”的資料頁面http://localhost:52270/Category/ManageDetails/6
修改欄目視圖 
運行看下效果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET MVC5網(wǎng)站開發(fā)之登錄、驗證和注銷管理員篇1(六)
- ASP.NET MVC5網(wǎng)站開發(fā)之展示層架構(gòu)(五)
- ASP.NET MVC5網(wǎng)站開發(fā)管理列表、回復(fù)及刪除(十三)
- ASP.NET?MVC5網(wǎng)站開發(fā)咨詢管理的架構(gòu)(十一)
- ASP.NET?MVC5網(wǎng)站開發(fā)顯示文章列表(九)
- ASP.NET MVC5網(wǎng)站開發(fā)添加文章(八)
- ASP.NET MVC5網(wǎng)站開發(fā)用戶修改資料和密碼(六)
- ASP.NET?MVC5網(wǎng)站開發(fā)用戶登錄、注銷(五)
- ASP.NET?MVC5?網(wǎng)站開發(fā)框架模型、數(shù)據(jù)存儲、業(yè)務(wù)邏輯(三)
- ASP.NET?MVC5網(wǎng)站開發(fā)項目框架(二)
相關(guān)文章
ASP.NET中利用Segments取得URL的文件名的一種方法分享
在ASP.NET中,取得請求頁的URL地址有多種方式,其中有一種方式取得網(wǎng)頁文件名。2011-09-09
Asp.net中將Word文件轉(zhuǎn)換成HTML的方法
這篇文章主要介紹了Asp.net中將Word文件轉(zhuǎn)換成HTML的方法,需要的朋友可以參考下2014-08-08
解決.Net Core項目發(fā)布在IIS上訪問404的問題
這篇文章介紹了解決.Net Core項目發(fā)布在IIS上訪問404的問題,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
asp.net結(jié)合Ajax驗證用戶名是否存在的代碼
關(guān)于Ajax的操作簡單總結(jié),結(jié)合Ajax驗證用戶名是否存在的代碼2010-06-06
.net MVC使用IPrincipal進行Form登錄即權(quán)限驗證(3)
這篇文章主要為大家詳細(xì)介紹了.net MVC使用IPrincipal進行Form登錄即權(quán)限驗證,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
.net core日志系統(tǒng)相關(guān)總結(jié)
多年的經(jīng)驗,日志記錄是軟件開發(fā)的重要組成部分。沒有日志記錄機制的系統(tǒng)不是完善的系統(tǒng)。在開發(fā)階段可以通過debug附件進程進行交互調(diào)試,可以檢測到一些問題,但是在上線之后,日志的記錄起到至關(guān)重要的作用。本文講解下日志系統(tǒng)的相關(guān)使用2021-06-06
.net?core?中?WebApiClientCore的使用示例代碼
這篇文章主要介紹了.net?core?中?WebApiClientCore的使用示例代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
在Framework4.0中實現(xiàn)延遲加載的實現(xiàn)方法
延遲加載,亦稱延遲實例化,延遲初始化等,主要表達的思想是,把對象的創(chuàng)建將會延遲到使用時創(chuàng)建,而不是在對象實例化時創(chuàng)建對象,即用時才加載。2011-08-08

