ASP.NET Core MVC's view (Views)

  ASP.NET Core MVC controller may use the view returns formatted results.

 

  1. What is the view

  In the MVC, the view encapsulates the user interacts with the presentation details. Is a view to generating the content sent to the client, HTML template contains embedded code. Razor view that use syntax that allows minimal code complexity or interact with HTML.

  ASP.NET Core MVC view files default to Views .cshtml file is saved in the application's folder inside. Typically, each controller has its own folder, wherein the controller comprises a corresponding method of operation. FIG.

  Corresponding to a view of the operation, partial view of the layout view, and other specific file can be used to reduce duplication, and allows the repeated use in the view.

  Providing an isolated view MVC interest in application, the user interface and business logic level mark packaged separately. Generally, <layout> Layout View or <partial> corresponding to the user interface layout and reuse by sharing instruction.

 

  2. Create a view  

  View belong to a controller created in the Views / [ControllerName] file in the folder. Between the controller is placed in the common view / Views / Shared folder. Name the view controller associated with the operation of the same name, and add .cshtml extension. For example, Home About operation of the controller to create a view, you should create a About.cshtml file in / Views / Home folder, or right-click on the Add method of operation view:

@{
    ViewData["Title"] = "About";
}
<h2>@ViewData["Title"]</h2>
<h3>@ViewData["Message"]</h3>

<p>Use this area to provide additional information.</p>

  @ Symbol represents Razor code. C # syntax {} Razor operation code block wrapped in Razor values ​​can be manipulated by using the @ symbol to display in HTML, as above <h2> and inside the element as shown <h3>.

  This view only care about this part of the output of its responsibility. The portion of the page layout, as well as view common look and feel, specified elsewhere.

 

  3. The controller specifies the view 

   A view typically returned from the operation as ViewResult. Operation can be created directly and return a ViewResult, but in general, if the controller inherits from Controller, you can simply use the View helper method:

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

  The method has a plurality of auxiliary View overloaded, so as to return to that view. Can optionally specify a return view, the view may also be returned to a model object.

  When the operation returns to the view, the view will occur a process called discovery. This process will determine which view files. Unless you specify a particular view file, otherwise the corresponding view controller first looks run-time, and then find the name of the view match in the Share folder.

  When the operation returns View methods, such as return View (); this operation are used as the name of the view names. This method can also give a clear view of the bed name return View ( "SomeView");. In both cases the view controller will view the file in the corresponding folder and file search Share folders match.

  May provide a path to the file view, in this case, .cshtml extension must be specified as part of the file path, return View ( "Views / Home / About.cshtml");

 

  4. The data transferred 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 (commonly referred to as model view, to distinguish the type of business model) and then transmitted from the examples of this type of operation to view. Recommended model or view model passes the data to the view. This allows the use of strong type checking view, a view command may be used to specify @model Model:

@model MVCTest.Models.Operation

@{
    ViewData["Title"] = "Create";
}

<h2>Create</h2>

<h4>Operation</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Id" class="control-label"></label>
                <input asp-for="Id" class="form-control" />
                <span asp-validation-for="Id" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

  Once the model specified for the view, can be used in a strongly typed access @Model instance sent to view. Use (@ Model.Name) above the label assistant. The controller provides a view Example:

        public ActionResult Create()
        {
            var model = new Operation()
            {
                Id=1,
                Name="test"
            };
            return View(model);
        }

  For type can not be provided to limit the view as a model, a view model recommended transmission with little or no action, other business logic in order to position the package in the application.

 

  1. weakly typed data

   In addition to the strongly typed view, all views can access the data set of the weak type. This set may be referenced by ViewData or ViewBag properties on the controller and views. ViewBag belonging ViewData a wrapper that provides a dynamic view of the set, it is not a separate collection.

  ViewData key is accessed by a string dictionary object, which can store and retrieve objects. When the object is extracted, they need to be converted to a particular type. ViewData may be used to transfer data from the controller to the view, and the transmission in a view (partial view and layout) in the. Strings can be stored and used directly, no 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";
    //需要转化
    var operation = ViewData["Operation"]  as Operation ;
}
<h2>@ViewData["Title"]</h2>
<h3>@ViewData["Message"]</h3>
<h3>@operation.Id</h3>
<h3>@operation.Name</h3>
<p>Use this area to provide additional information.</p>

  ViewBag object provides dynamic access to objects stored in the ViewData, so can be more convenient to use because no conversion:

@{
    ViewData["Title"] = "About";

}
<h2>@ViewData["Title"]</h2>
<h3>@ViewData["Message"]</h3>
<h3>@ViewBag.Operation.Id</h3>
<h3>@ViewBag.Operation.Name</h3>
<p>Use this area to provide additional information.</p>

  Since both point to the same underlying ViewData set, so if convenient, can read and write values ​​at the time, and between ViewBag ViewData can be mixed.

 

  2. Dynamic Views

   Not declared type, but with a view to transmitting them model instances may be dynamically referenced to this example. However, the compiler does not provide any protection or IntelliSense. If the property does not exist, the page will fail at run time.

  

  The more characteristic view

  Tag assistant can easily be added to an existing server-side behavior of the HTML tags, avoiding the use of custom code or in a view helper code.

  Generating a custom HTML tags can be built using a number of HTML helper achieved, more complicated UI logic (the data may have its own requirements) may be encapsulated assembly view (View Components) In. View component provides the same focus and the view provided to the controller, and can eliminate the need for data processing operations and views by using the common UI elements.

  View page support dependency injection, allowing service injected into view. (It will be mentioned later)

 

 

 

 

Guess you like

Origin www.cnblogs.com/afei-24/p/11216803.html