In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to use the view in ASP.NET Core MVC". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The ASP.NET Core MVC controller can use the view to return the formatted results.
1. What is a view?
In MVC, the view encapsulates the rendering details of the user's interaction with the application. The view has an HTML template that generates content to be sent to the client and contains embedded code. The view uses Razor syntax, which allows you to interact with HTML with minimal code or complexity.
The ASP.NET Core MVC view is saved as a .cshtml file in the application's Views folder by default. Typically, each controller has its own folder that contains a view of how the controller operates.
In addition to the corresponding views, detail views, layouts, and other specific view files can be used to reduce duplication and allow reuse in the view.
Views provide separation of concerns in MVC applications, encapsulating user interface-level tags from business logic. Typically, it can be reused through the user interface where the layout corresponds to shared instructions or layout views.
two。 Create a view
Views that belong to a controller are created under the Views/ [ControllerName] folder. Views that are common between controllers are placed under the / Views/Shared folder. Name the view the same name as its associated controller operation and add the .cshtml extension. For example, to create a view for the About operation of a Home controller, create an About.cshtml file under the / Views/Home folder, or right-click on the action method to add a view:
@ {ViewData ["Title"] = "About";} @ ViewData ["Title"] @ ViewData ["Message"]
Use this area to provide additional information.
The @ symbol represents the Razor code. The C # syntax runs in a block of Razor code wrapped in {}, and Razor can be displayed in HTML by manipulating the value using the @ symbol, as shown above and in the element.
This view is only concerned with the part of the output that it is responsible for. The part of the page layout, as well as the general appearance in the view, is specified elsewhere.
3. Controller assigns views
The view is usually returned from the operation as a ViewResult. The action method can directly create and return a ViewResult, but in general, if the controller inherits from Controller, you only need to use the View helper method:
Public IActionResult About () {ViewData ["Message"] = "Your application description page." return View ();}
View's helper methods have multiple overloads to make it easy to return to the view. You can optionally specify a returned view and return a model object to the view.
When the operation returns to the view, a process called view discovery occurs. This procedure determines which view file to use. Unless a specific view file is specified, the runtime first looks for the view corresponding to the controller, and then looks for a matching view name in the Share folder.
When an operation returns a View method, such as return View (); the name of the operation is used as the view name. You can also give this method an explicit view name return View ("SomeView"); under the bed. In both cases, the view searches the corresponding controller's view folder and Share folder for matching files.
The path to the view file can be provided, in which case the .cshtml extension must be specified as part of the file path, return View ("Views/Home/About.cshtml")
4. Pass data to the view
ASP.NET Core MVC can use a variety of mechanisms to pass data to the view. The most robust way is to specify a model type in the view (often referred to as the view model to distinguish it from the model type of the business domain), and then pass an instance of that type from the operation to the view. It is recommended that you use a model or view model to pass data to the view. This allows the view to take advantage of strongly typed checking, and you can use the @ model directive to specify a model for the view:
@ model MVCTest.Models.Operation@ {ViewData ["Title"] = "Create" } CreateOperation Back to List
Once a model is specified for the view, you can use @ Model to access the instance sent to the view in a strongly typed manner. (@ Model.Name) the tag assistant used above. The controller provides an example to the view:
Public ActionResult Create () {var model = new Operation () {Id=1, Name= "test"}; return View (model);}
There are no restrictions on the types that can be provided to the view as a model, and it is recommended that you pass a view model with little or no behavior in order to encapsulate the business logic elsewhere in the application.
1. Weakly typed data
All views have access to weakly typed data collections except for strongly typed views. This collection can be referenced by the ViewData or ViewBag property on the controller and view. ViewBag is a wrapper for ViewData that provides a dynamic view of the collection, which is not a separate collection.
ViewData is a dictionary object accessed through string keys in which objects can be stored and retrieved. When you extract objects, you need to convert them to specific types. You can use ViewData to transfer data from the controller to the view, as well as in views (detail views and layouts). Strings can be stored and used directly without conversion:
Public IActionResult About () {var model = new Operation () {Id=1, Name= "test"}; ViewData ["Operation"] = model; ViewData ["Message"] = "Your application description page."; return View ();}
View:
@ {ViewData ["Title"] = "About"; / / need to convert var operation = ViewData ["Operation"] as Operation;} @ ViewData ["Title"] @ ViewData ["Message"] @ operation.Id@operation.Name
Use this area to provide additional information.
The ViewBag object provides dynamic access to objects stored in ViewData, which makes it easier to use because there is no need for conversion:
@ {ViewData ["Title"] = "About";} @ ViewData ["Title"] @ ViewData ["Message"] @ ViewBag.Operation.Id@ViewBag.Operation.Name
Use this area to provide additional information.
Because both point to the same underlying ViewData collection, you can mix ViewBag and ViewData when reading and writing values, if convenient.
two。 Dynamic view
Types are not declared, but views with model instances passed to them can refer to this instance dynamically. However, no compilation protection or IntelliSense is provided. If the property does not exist, the page will make an error at run time.
5. More View Properti
The tag assistant can easily add server-side behavior to existing HTML tags, avoiding the use of custom code or helper code in the view.
Custom HTML tags can be generated using many built-in HTML helpers, and more complex UI logic (which may have its own data requirements) can be encapsulated in a view component (View Components). View components provide the same concerns as controllers and views, and eliminate the need to handle actions and views that consume data by common UI elements.
The view page supports dependency injection, allowing service injection into the view.
That's all for the content of "how to use views in ASP.NET Core MVC". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.