ASP. net mvc 4 Practice Study Note 3: view,
I did not understand the last two sections of this article, but I still need to spend some time. Let's take a transcript and wait for a while before learning about it...
The survival goal of a view is to get a model for it and use this model to render the content. Since the controller and related services have executed all the business logic and packaged the results into a model object, the view only needs to know how to obtain the model and convert it to HTML.
1. Select the view to be rendered:
The View is rendered by calling the View method in the Controller action. As mentioned above, the Create action in GuestbookController:
public ActionResult Create(){ return View();}
When the View method is called without parameters, the default rendering View name of the framework is the same as the action name (Create, the default view engine is located in the Views/<controller Name> and Views/Shared directories.
If the View to be rendered is different from the action name, you need to pass the parameter to the View method, for example, New. cshtml, return View ("New "). If the View is not in a subdirectory with the same name as the controller, You need to specify the relevant path, such as return View ("~ /Views/SomeOtherDirectory/New. cshtml ").
2. transmit data to the View:
There are three ways to pass data to a view: ViewDataDictionary (View data dictionary), ViewBag (View package), and forced type view.
1. ViewDataDictionary:
Modify the Details action and Details View as follows:
1 public ActionResult Details (int? Id) 2 {3 if (id = null) 4 {5 return new HttpStatusCodeResult (HttpStatusCode. badRequest); 6} 7 GuestbookEntry guestbookentry = db. entries. find (id); 8 if (guestbookentry = null) 9 {10 return HttpNotFound (); 11} 12 bool IsShowEdit = guestbookentry. name = "Zhang Wuji"; 13 ViewData ["IsShowEdit"] = IsShowEdit; 14 return View (guestbookentry); 15}
1 @ * omitted above * @ 2 <p> 3 @ {4 bool IsShowEdit = (bool) ViewData ["IsShowEdit"]; 5} 6 @ if (IsShowEdit) 7 {8 @ Html. actionLink ("Edit", "Edit", new {id = Model. id}) 9} 10 @ Html. actionLink ("Back to List", "Index") 11 </p>
We can store any data in ViewDataDictionary, but the data received from the dictionary must undergo type conversion.
2. ViewBag:
The preceding Details action and Details view can be changed:
ViewBag.IsShowEdit = IsShowEdit;
@if(ViewBag.IsShowEdit) { @Html.ActionLink("Edit", "Edit", new { id = Model.Id }) }
ViewBag does not require type conversion and can be directly used.
The advantage of ViewData and ViewBag is flexibility. The disadvantage is that the compiler cannot detect errors if the dynamic attribute name is incorrect during use. In addition, metadata cannot be added to dynamic attributes, such as annotation attributes.
3. Strong type View:
You can use the @ model keyword to specify the model type to obtain a strong type, such as in the Index View:
@model IEnumerable<Guestbook.Models.GuestbookEntry>
You can also write it as follows:
@using Guestbook.Models@model IEnumerable<GuestbookEntry>
We can use the HtmlHelper extension method designed for a strong-type view to display the strong-type view data. The list is as follows:
Iii. Use a strong template:
The HtmlHelper extension method is suitable for some HTML element fragments. However, when the generated HTML becomes more complex and contains various elements, it is generally not prone to extended use. The MVC team also designed Templated Helper to help generate HTML based on strong types.
1. DisplayFor and EditorFor templates:
- Html. Display ("Message"); -- weak type view Display template
- Html. Display (m => m. Message); -- Display template based on the strong expression type view
- Html. DisplayForModel (); -- generates a complete display view template for each member in the iterative model.
- Html. Editor ("UserName"); -- weak type view editing template
- Html. EditorFor (m => m. UserName); -- edit a template based on the strong expression View
- Html. EditorForMode (); -- generates a complete editing view template for each member in the iteration model.
2. built-in template:
Except the Collection and Object Templates, each template renders a single value. The Collection Template cyclically traverses each data item in the model Object, and the Object template cyclically traverses each data item in the ModelMetadata. Properties set.
3. Select a template: [not too familiar at the moment...]
In essence, the method for displaying templates and editing templates is to use a specific algorithm to find the template by name.
The template search location is the DisplayTemplates and EditorTemplates folders. Like the names of some views and views, the template method first views the controller (or "region + controller") before turning to the Shared view folder ") A dedicated view folder. Take template editing as an example. If the template helper method is used in the region-specific view, these folders include:
- <Area>/<controller Name>/EditorTemplates/<Template Name>. cshtml (or. vbhtml );
- <Area>/Shared/EditorTemplates/<Template Name>. cshtml
If the template is not found in these folders or the view is not in the region, the default view search position is used:
- <Controller Name>/EditorTemplates/<Template Name>. cshtml (or. vbhtml );
- Shared/EditorTemplates/<Template Name>. cshtml
4. Custom template: [do not fully understand, cry...]
Advantages of model templates built on some views:
1) Some views need to be selected by name in the view, and the template is selected by model metadata information. You do not need to specify which template to use for the view;
2) ViewDataDictionary provides additional information for the template. Some views and other pages cannot receive this information, which is in the ViewData. ModelMetadata attribute.
You can use the ModelMetadata attribute to access all Metadata information generated by the Model Metadata Provider, including Model type information, attributes, and Model Metadata.
In addition to the general model type information, the ModelMetadata object also includes other metadata, which are filled by annotation attributes by default.
The ModelMetadata object also exposes the AdditionalValues attribute of the IDictionary <string, object> type, which can contain additional metadata information filled by the preset metadata.
How does ASPNet MVC3 obtain the value on The View?
What do you do with this path. After you upload the file to controllers, your file will be compiled into a binary stream containing the file name, file size, and file size. I think the json data will be sent to the controller. Therefore, the normal upload cannot obtain the client path.. To ensure security, you do not need to obtain the file path of the customer service.
ASPNET MVC30 RAZOR mode returns multiple sets bound to the VIEW
No one will answer you here. If you don't even know what MVC is, you don't understand it. You just need to know that there is no DataSet in MVC3,
MVC advocates that all data is transmitted using models, that is, mvc m (model ).
I can go to many tutorials online, and I will do some exercises for a few hours.