2010.9.4 su Peng
Content
-HtmlHelper class
-In-depth introduction to ViewEngine
Prerequisites
-Install Visual Studio 2010 Express
-Understand ASP. Net
-Understand the basic concepts of the Design Mode
Html. ActionLink and Html. RouteLine
-Both controls generate navigation information based on user input.
-ActionLink: call another Controller Link based on Generation
Action of the specified navigation
Specify the Controller and Action of the navigation
Parameter of Action access
Other parameters
RouteLink
Html. BeginForm
-This tag fully simulates the <form> tag
The BeginForm tag is slightly different from other HtmlHelper tags. Other HtmlHelper tags appear in a single way, while the BeginForm tag appears in pairs, it completely imitates the <form> </form> used in html.
Method 1
Method 2 (recommended)
Html. Hidden
-Used to generate hidden text
Html. DropDownList and Html. ListBox
-Used to display the list or select a value
-Restrict user input
Strong Type Support
Html. PassWord
-Password works in the same way as textbox, but data is automatically cleared after submission.
(Correct: the value of the id attribute of the input control in should be my_upwd)
Html. RadioButton
-Generate a set of selection values and require the user to select one.
Html. Partial and Html. RenderPartial
-Used to output partial html fragments
It has four types of overloading
Usage
Note:
RenderPartial is a direct Response. Write output, so <%> the colon is not required.
The Partial is escaped and needs to be compiled. Therefore, <%: %> there is a colon.
RenderPartial and Partial are essentially the same. The difference is that RenderPartial directly outputs Html, and Partial also outputs Html, but there is an escape in the middle. During use, RenderPartial actually uses Response. the Write method outputs data directly to the page. The performance of the Write method is much better than that of the Partial method in large access scenarios. Therefore, in many cases, we use the Write method to output data.
Html. Action and Html. RenderAction
-This interface is used to call a specified Action of a specified Controller.
There are several functions in the Controller. These functions are called actions. They generally return the ResultAction result for each Action in the Controller. The ResultAction will be placed in the View layer as the View, and their relationship is roughly like this. However, in some cases, if we want to output or call only the specified Action, we use the Html. Action method. The Partial we just introduced is to output the data in the View into an independent file, so the Action and RenderAction are to execute an Action in the independent Controller and return the result. Action provides a large number of extensible methods for implementation, because each ChildAction may have some Model data, and each independent Controller can be called together.
By default, in the returned View, the View must obtain data from a fixed Controller. It is difficult to obtain data from multiple controllers in one View, because Route does not allow this. For example, the View in the Home folder can only get data from HomeController.
In this way, it is more difficult, and Html. Action is specifically designed to solve this problem. If you declare an Attribute named ChildAction on the Action, you can use Html. Action to output this Action when calling View.
View of other controllers calls the Action of MyController
Note that once an Action is marked as ChildAction, it cannot be directly referenced by a Url. Another point is that the Controller has a Context control, which can determine whether an Action is ChildAction. If so, this Action cannot be added with other labels, such as authorization. If an Action is ChildAction, it can be referenced on any page and thus cannot be authorized in this case. This logic is also easy to understand.
The other one is that you cannot use OutputCache for configuration. Once OutputCache is cached, the system does not know which instance to use when you call this ChildAction on different pages.
RenderAction
-PASS Parameters
Because there is already an Action named Menu and there is no parameter, the following Menu overload will not be recognized by the View.
The correct method is to add an ActionName tag.
In this way, the Action name exposed to the outside is not a Menu but a coolmenu, avoiding the problem of duplicate names.
(Correct: the ActionName Of This call should be passed into coolmenu instead of Menu, and the call method should be Html. RenderAction)
Html. TextArea
-Used to generate <textarea> labels
It is used to pass text, not RichTextBox. It cannot display Html itself. If Html is output, it will perform Encoder escape on Html.
The row and column output can be specified.
Strong Type Support
Html. ValidationMessage
-Display ModelState verification results
It is similar to ViewState. ViewState transfers data between the View layer and the Controller. ModelState transfers data between the View layer and the Model layer. It is a container used to store some data results. Generally, the verification result is stored in ModelState and uploaded to the presentation layer. Data verification is best performed at the Model layer.
The classused to verify the error information is field-validation-error, which is defined in the contentfolder. There is a style.css file in it, and the style of the error information can be changed.
You can also specify the error message directly. The following example shows the error message "some code has errors" Once the Name of ModelState has a value.
Strong Type Support
-Display a group of Error Data
The main description parameters that can be passed in errors
Html Template
-ASP. Net MVC2 supports custom templates
View engine Introduction
The function of the View engine is mainly to return the View. The View is in any format that the user wants and can be stored anywhere the user wants.
This means that we need to generate An aspx file, which has various HtmlHelper to help display html instances, as well as scripts generated by a collection of other methods. Then, parse aspx to generate html, which is the work of the WebForm engine. It will call various aspx APIs to generate html.
Not all engines generate aspx pages, and not all views generate html. Some engines do not accept views, but receive other things. For example, you can customize some special DSL and output some other things.
Therefore, generating html is not the only choice, and receiving Controller Access is not the only entry.
Work Mode
Configure ViewEngine
-Write the ViewEngine configuration in Global. asax. cs.
The Application_Start function is the global application entry. Add your own engine in Application_Start
In fact, the first Clear statement can be left blank, because MVC supports multi-engine interaction.
So there is no need to clear the previous engine.
Use View
-IViewEngine Interface
FindView uses iterations to find the object of each ViewEngine. When a View is passed over, the Collection object of ViewEngine keeps asking each Engine: "Is the View name registered ?". If yes, you can solve the problem, if not for others. If none of them are found, null is returned.
FindPartialView is used for ChildAction.
FindView is used for global views.
The ReleaseView does not need to be mentioned, so the Response depends on it.
-IView Interface
Render is the method used for output.
ViewContext attributes
-HttpContext (Request, Response, Server, Session, etc)
-Controller (return the ControllerBase instance, indicating who called ViewEngine)
-RouteData (the returned Route object indicates the region from which the Route is routed)
-ViewData (the dictionary contains all data objects from the Controller. Render depends largely on the data retrieved from these objects)
-TempData (also contains some Controller objects, but usually Cache objects)
-View
-ClientValidationEnable)
-FormContext (including some attributes of the Form client)
-FormIdGenerator (Judge Form)
-IsChildAction (if it is true, use PartialView to Render; if it is false, use View to do it)
-ParentActionViewContext
-Write (output basic information)
Select a ViewEngine
-Advantages of the default WebFormViewEngine
-Similar to Webform
-Use masterpage
-Supports using C # Or vb.net to write scripts.
-Use System. Web. UI. Page
-VS2010 comes with smart sensing
Use different ViewEngine
-Eager to use different languages (such as Ruby or Python)
-We hope to get a simpler Html to support more browsers (with less styles)
-The output result is not Html, for example, to output xaml, rss, or PDF.
Spark
-Support Using Dynamic languages to generate Views such as IronPython and IronRuby
-Concise output
-Supports a technology similar to MasterPage
FAQs
-Use ViewEngine or ActionResult
For example, if you want to output a Json object, there is a JsonResult in MVC. If there is no JsonResult, you can only use reflection to look for attributes. In this case, it is more appropriate to write a JsonResult. If xllt is used to output xml into html, it is better to write an xllt ViewEngine if only one ActionResult is written.
If there is only one method, use ActionResult; If there are multiple methods, use ViewEngine.
2010.9.23