Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use MVC4 to make foreground column browsing

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to use MVC4 to make foreground column browsing, I believe many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

I. column

Browse the foreground column

The foreground page of the website, at the top to be able to display the root column, click the column name to enter the navigation of the important sub-column in the column, and there must be the current path in the column page. Do these three parts first.

1), root column

Open [CategoryController] and add [PartialRoot] Action

/ / root column / public ActionResult PartialRoot () {return View (categoryRsy.Root ());}

Right-click to add view model class to select Category, bracket template to select List, check to create partial view, OK.

Delete other codes except @ model IEnumerable at the top, and manually write the code as follows:

@ model IEnumerable@Html.ActionLink ("website homepage", "Index", "Home") @ foreach (var item in Model) {@ Html.ActionLink (item.Name, "Index", "Category", new {id = item.CategoryId}, null)}

2), sub-column navigation

Add [PartialChildren (int id)] Action to [CategoryController]

/ Sub-column / public ActionResult PartialChildren (int id) {return View (categoryRsy.Children (id));}

Right-click to add partial view

Model IEnumerable @ foreach (var item in Model) {@ Html.ActionLink (item.Name, "Index", "Category", new {id = item.CategoryId}, null)}

3), path

Add [PartialPath (int id)] Action to [CategoryController]

/ / column path / public ActionResult PartialPath (int id) {List _ path = new List (); var _ category = categoryRsy.Find (id); while (_ category! = null) {_ path.Insert (0, _ category); _ category = categoryRsy.Find (_ category.ParentId);} return View (_ path);}

Right-click to add partial view

@ model IEnumerable your current location: @ Html.ActionLink ("website Home", "Index", "Home") @ foreach (var item in Model) {@ Html.Raw (">") @ Html.ActionLink (item.Name, "Index", "Category", new {id = item.CategoryId}, null)}

We can see the effect soon.

Open the Layout\ _ Layout.cshtml layout page and add @ Html.Action ("PartialRoot", "Category") to the top navigation location.

Open http://localhost:52270/Category/ManageAdd and add a few columns.

Run it and take a look at the home page of the website

It worked!

Start working on the Index index page

Add [Index (int id)] Action to [CategoryController]

If the column Type=2 then jump to Navigation, otherwise return to the CategoryView view.

/ Index / public ActionResult Index (int id) {var _ category = categoryRsy.Find (id); if (_ category = = null) {Error _ e = new Error {Title = "error", Details = "specified column does not exist", Cause = "the column you visited has been deleted", Solution = Server.UrlEncode ("return to the home page of the site")} Return RedirectToAction ("Error", "Prompt", _ e);} if (_ category.Type = = 2) return Redirect (_ category.Navigation); return View (_ category.CategoryView,_category);}

Add enhanced type view

@ model Ninesky.Models.Category@ {ViewBag.Title = "column default page"; Layout = "~ / Views/Layout/_Layout.cshtml";}

Model.Name @ Html.Action ("PartialChildren", "Category", new {id = Model.CategoryId}) @ Html.Action ("PartialPath", "Category", new {id = Model.CategoryId})

This is the default page of the column.

Copy a view with Index.cshtml named IndexSingle.cshtml as a single-page column

Then make a copy of Index.cshtml named IndexAbout.cshtml as a dedicated view about our column, and modify the corresponding code

Model Ninesky.Models.Category@ {ViewBag.Title = "about us"; Layout = "~ / Views/Layout/_Layout.cshtml";}

Model.Name @ Html.Action ("PartialChildren", "Category", new {id = Model.CategoryId}) @ Html.Action ("PartialPath", "Category", new {id = Model.CategoryId}) @ Model.Name About

NineSky ®is a project of Dongting Xizhao Learning Mvc. I want to constantly urge myself, keep learning and practice by completing a website. Finally, I hope to write a concise and easy-to-use website.

Objective: to learn mvc4

Goal: simple, easy to use and practical

Open the "about us" information page http://localhost:52270/Category/ManageDetails/6

Modify column view

Run it and see the effect.

After reading the above, have you mastered how to use MVC4 to make foreground column browsing? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report