stay MVC Of Razor View shows the way of stepless classification , I read a lot of information on the Internet , Most of them are very tall . Maybe my level is limited , I really can't use it .

Then I'll do it in the simplest way .

Model:

 public class NewsCategory
{
[Key]
public int CategoryId { get; set; }
public int ParentCategoryId { get; set; }
[Required]
[StringLength()]
public string CategoryName { get; set; }
}

ViewModel

 public class NewsCategoriesViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public List<NewsCategoriesViewModel> children { get; set; }
}

Controller

Get data recursively , And then back to the view

 abcContext db = newabcContext();
public ActionResult Index()
{
var categoryList = GetCategoryList();
return View(categoryList);
} [NonAction]
public List<NewsCategoriesViewModel> GetCategoryList(int Id)
{
List<NewsCategoriesViewModel> uvModel = new List<NewsCategoriesViewModel>(); var perentList = db.Set<NewsCategory>().Where(p => p.ParentCategoryId == Id).ToList(); if (perentList.Count > )
{
foreach (var item in perentList)
{
NewsCategoriesViewModel userViewModel = new NewsCategoriesViewModel
{
Id = item.CategoryId,
Name = item.CategoryName,
children = new List<NewsCategoriesViewModel>()
};
List<NewsCategoriesViewModel> tempList = GetCategoryList(item.CategoryId);
if (tempList.Count > )
{
// There's something wrong here ;
//userViewModel.children.Add(tempList);
userViewModel.children = tempList;
}
uvModel.Add(userViewModel);
}
}
return uvModel;
}
}

View

Define a view method , Then recursively call .

@model List<NewsCategoriesViewModel>
@helper DisplayList(List<NewsCategoriesViewModel> model)
{
if (model.Count > )
{
<ul>
@foreach (var item in model)
{
<li>@item.Name</li>
if (item.children.Count > )
{
@DisplayList(item.children);
}
}
</ul> }
}
@DisplayList(Model)

It's the end of the game !

MVC in Razor More related articles displayed in unlimited categories

  1. MVC in Razor Use And the path

    grammar : @ You can write a C# sentence @{} You can write a set of C# sentence @: Output the text content directly to the page @() Put a paragraph in a sentence C# Code included , Prove that this sentence is complete C# Code Reference namespace :@using Space name H ...

  2. From a project manager's point of view .net Of MVC in Razor Grammar is really rubbish .

    We know ,Razor In grammar we can use @if(){} And so on , This makes .net It's easier for programmers to write templates . The comparison is as follows : Syntax name Razor  grammar Web Forms  Equivalent grammar Code block ( Server side ) @{ ...

  3. MVC in Razor View basic syntax (1)

    Razor front , There has to be a gap with the front one , That is, the space ( Thank you for reminding me on the first floor ,url There are no spaces in it , If you want to in url It just needs to be @(ViewBag.), Just add the brackets ), The following must be coherent , Otherwise, add parentheses 1, Output the order in the page ...

  4. mvc in razor One of the bug

    I won't say more about specific things , All compilers , There's something wrong with the code . As a result, when previewing the page, I was told that there was a compilation error , Nima, this is unscientific . Take a look at the error page Looking at the problem, it should be the lack of } , Or more } An inverted compilation error is right , But the compilation is done ...

  5. MVC in Razor Engine learning :RenderBody,RenderPage and RenderSection

    RenderBody stay Razor There's no more in the engine “ master page ”, Instead, it's called “ Layout ” The page of (_Layout.cshtml) In the shared view folder . In this page , Will see There's a statement in the tag : @Rend ...

  6. ASP.NET Core 5.0 MVC View categories in —— Layout view 、 Launch view 、 Specific view 、 Division of the view

    One . establish MVC Applications The created project Two .( Overall ) Launch view _ViewStart.cshtml seeing the name of a thing one thinks of its function , Is in the View Before starting execution , And every one of them View, Its default content is @{ Layout ...

  7. MVC In the RAZOR Suddenly in the template CANNOT RESOLVE SYMBOL ‘VIEWBAG’ Error prompt for

    And then in Razor In the @ViewBag Not available ,@Url Unavailable ,@Html None of these variables can be used . Abnormal prompt : Compiler error messages : CS0426: type “XX.Model.System” Type name does not exist in ...

  8. ASP.NET Core 5.0 MVC Medium Razor page Introduce

    Razor Is a markup syntax for embedding server based code into web pages . Razor Grammar by Razor Mark .c # and HTML form . Usually contains Razor The extension of the file cshtml Razor grammar ...

  9. ASP.NET MVC 3: Razor Medium @: And grammar

    original text  ASP.NET MVC 3: Razor Medium @: And grammar [ The original address ] ASP.NET MVC 3: Razor’s @: and <text> syntax[ When the original text was published ] De ...

Random recommendation

  1. 5sing analysis

    0x1. Grab data : use fiddler You can't grab , use smartsniff [6/26/ Sunday 18:05:04:391]GET /user/login?username=15081515272&pa ...

  2. JAVA Object oriented preliminary knowledge summary : encapsulation 、 Inherit 、 polymorphic

    1. encapsulation Wrap data and methods into classes , And the specific implementation of the hidden , It's often called encapsulation . The result is a data type with both characteristics and behavior . The so-called specific implementation of the hidden is achieved through access control .JAVA When subclasses override inherited methods , Can not be ...

  3. jetty and tomcat Start project

    First jetty and tomcat difference , Not quite , Just say what I understand .jetty Architecture tomcat It's simpler .jetty Is based on Handler To achieve , Easy to expand , Therefore, it is more suitable for simultaneous processing and keeping connection for a long time :tomcat The frame of ...

  4. static Member functions

    1.static Member functions cannot be defined as const Of , This is because static A member function is just a formal encapsulation of a global function , and static Member functions cannot access non static members of a class ( No, this) The pointer , How to modify non static data members ...

  5. CentOS install Jexus

    Official website :http://www.jexus.org/ Just follow the page in the installation process , The premise is that it needs to be installed mono stay VS2015 Create a new one in MVC Applications , There are two steps to note here : The first 1 Step : remove bin Under the Micr ...

  6. ExtJS Get browser width

    JS In the code : Ext.onReady(function() { var width=window.screen.availWidth; var height=window.screen.availHe ...

  7. iOS Baidu Maps Small features demo

    Let's start with the picture above watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDEyMzIwOA==/font/5a6L5L2T/fontsize/400/fill ...

  8. SQL learning SELECT Clause order

    Let's sum up all the things mentioned in the previous essay SELECT Order of clauses . Clause explain Whether it is necessary to use SELECT                  The column or expression to return                       ...

  9. lambda, reduce, map Find the sum of factorials

    After learning these elegant built-in functions , You can do some interesting exercises to stimulate your interest . and python The biggest advantage is simplicity , Look at the following requirements use 1 Line code 1! + 2! + 3! + ... + 10! Find the factorial reduce Function use ...

  10. php Upload files $_FILES[&#39;&#39;][&#39;type&#39;] Value

    php Upload files $_FILES['']['type'] Value A function function upload_file($fname,$ftype,$fsize,$ferror,$ftmp_name,$fp ...