MVC Core website development (Ninesky) 2.1, foreground display (Supplement) of the topic, mvcninesky
In section 2.1, the front-end display of the topic despised Microsoft due to right-clicking without adding a view. Later, I carefully studied and found that I should despise myself. In fact, this function is actually available, I didn't figure it out.
In fact, you only need to install two packages (Microsoft. VisualStudio. Web. CodeGeneration. Tools and Microsoft. VisualStudio. Web. CodeGenerators. Mvc) in NuGet. For example:
Is it that you are familiar with it ..
You can now right-click to add a view in the pop-up dialog box. In actual useTemplate,AvailableCreate,EditIt can be used normally.
And selectDelete,DetailsAndListThe error may occur. I haven't completed the problem after two days. Please leave a message or send a message on QQ342211509. Thank you.
Split line ==================================================== ==============================
The data display of the topic is implemented last time. In fact, the topic does not display the content, but is processed in different ways according to the different topic types. For example:
1. add error display
First, the error page is displayed to facilitate later use.
1. Error Model
InNinesky. WebRight-click and choose>Add->Folder, Enter the nameModels.InModelsRight-click and choose>Add->Class, NameError
1 namespace Ninesky. web. models 2 {3 /// <summary> 4 // Error Model 5 /// </summary> 6 public class Error 7 {8 /// <summary> 9 // /error 10 /// </summary> 11 public string Title {get; set;} 12 13 // <summary> 14 // Name 15 /// </summary> 16 public string Name {get; set ;} 17 18 /// <summary> 19 // Description 20 /// </summary> 21 public string Description {get; set;} 22} 23} 24
2. Error View
Open View Views \ Shared \ Error. cshtml and modify it to the following code:
1 @model Ninesky.Web.Models.Error 2 @{ 3 ViewData["Title"] = Model.Title; 4 } 5 6 2. Improve the Column Display Action and view 1. Modify the Column Display code.
Open Controllers \ CategoryController. cs and modify the Index Code as follows:
1 /// <summary> 2 /// view Topic 3 /// </summary> 4 /// <param name = "id"> topic Id </param> 5 /// <returns> </returns> 6 [Route ("/Category/{id: int} ")] 7 public IActionResult Index (int id) 8 {9 var category = _ categoryService. find (id); 10 if (category = null) return View ("Error", new Models. error {Title = "Error message", Name = "", Description = "ID id 【 [" + ID + "], an Error occurs, and this topic does not exist. "}); 11 switch (category. type) 12 {13 case CategoryType. general: 14 if (category. general = null) return View ("Error", new Models. error {Title = "Error message", Name = "incomplete topic data", Description = "unable to find topic [" + category. detailed data of Name +. "}); 15 return View (category. general. view, category); 16 case CategoryType. page: 17 if (category. page = null) return View ("Error", new Models. error {Title = "Error message", Name = "incomplete topic data", Description = "unable to find topic [" + category. detailed data of Name +. "}); 18 return View (category. page. view, category); 19 case CategoryType. link: 20 if (category. link = null) return View ("Error", new Models. error {Title = "Error message", Name = "incomplete topic data", Description = "unable to find topic [" + category. detailed data of Name +. "}); 21 return Redirect (category. link. url); 22 default: 23 return View ("Error", new Models. error {Title = "Error message", Name = "topic data Error", Description = "topic [" + category. name + "] type error. "}); 24 25} 26}
The code is displayed according to the three types of columns.
2. View implementation
General topic View
Open Views \ Category \ Index. cshtml. The modification code is as follows:
1 @ model Ninesky. base. category 2 @ {3 ViewData ["Title"] = Model. name; 4} 5 6
Single-page column View
Copy and rename the Index of the regular column view to Page. cshtml, and modify the code to display the single-Page model content.
1 @ model Ninesky. base. category 2 @ {3 ViewData ["Title"] = Model. name; 4} 5 6
3. Improve data storage code
Open the BaseRepository. cs of Ninesky. DataLibrary and modify the Find method. The modified code is as follows:
1 /// <summary> 2 /// query 3 /// </summary> 4 /// <param name = "keyValue"> Primary Key </param> 5 // /<returns> object </returns> 6 public virtual T Find (params object [] keyValue) 7 {8 return _ dbContext. set <T> (). find (keyValue ); 9} 10 11 /// <summary> 12 // query 13 /// </summary> 14 /// <param name = "predicate"> query expression </param> 15 // <returns> entity </returns> 16 public virtual T Find (Expression <Func <T, bool> predicate) 17 {18 return Find (null, predicate ); 19} 20 21 /// <summary> 22 // query 23 /// </summary> 24 /// <param name = "includeParams"> navigation attributes </param> 25 // <param name = "predicate"> query expression </param> 26 // <returns> Object </returns> 27 public virtual T Find (string [] includeParams, expression <Func <T, bool> predicate) 28 {29 var queryable = _ dbContext. set <T> (). asQueryable (); 30 if (includeParams! = Null) 31 {32 foreach (string param in includeParams) 33 {34 queryable = queryable. Include (param); 35} 36} 37 return queryable. SingleOrDefault (predicate); 38}
_ DbContext. Set <T> (). Find (keyValue); queries the object based on the primary key and queries the cache first.
SingleOrDefault (predicate); query by expression:
- Public virtual T Find (string [] includeParams, Expression <Func <T, bool> predicate)-with navigation attributes
- Public virtual T Find (Expression <Func <T, bool> predicate)-without navigation attributes
4. improve business logic
To projectNinesky. BaseOpen CategoryService and modify the Find method.
1 /// <summary> 2 /// search 3 /// </summary> 4 /// <param name = "Id"> topic Id </param> 5/ // <returns> </returns> 6 public Category Find (int Id) 7 {8 return _ baseRepository. find (new string [] {"General", "Page", "Link"}, c => c. categoryId = Id); 9}
V. input test data
OpenSqlserver object Resource Manager, Find the Ninesky Database
Manually add data to the Categories, CategoryGeneral, CategoryPage, and CategoryLink tables, for example:
Press F5 in VS to run. Enter Id = 2 in the browser to display regular columns
Id = 3 display
Jump if Id = 5.
6. Other code hosting address: https://git.oschina.net/ninesky/Ninesky
Article published by: http://www.ninesky.cn
http://mzwhj.cnblogs.com/
Download code package: 2.1..rar
Returned directory