20 years old, afraid of what.
My feelings.
Speaking of a recent experience of using ANGULARJS, we used MVC for project development to understand and often use HTML Help classes to complete most of our front-end code writing, in fact, before I touch Angularjs This method is still very like, After all, it's something that's going to revolutionize the. aspx page, but as you use ANGULARJS in your project, I'm more and more fond of this development model that separates the front and back, and I believe this is the future development situation. But for now, this MVC is going to be used quickly.
: Understanding the inner beauty of HTML helper classes
The HTML helper method uses the routing engine to find the HomeController controller's search operation, and he uses the GetVirtualPath method in the background, which in the routes attribute of RouteTable, In Global.asax, the Web application registers all the routing locations.
protected void Application_Start () { arearegistration.registerallareas (); Webapiconfig.register (globalconfiguration.configuration); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (bundletable.bundles); }
Through Routeconfig.registerroutes (routetable.routes), we implement the auxiliary method. Let's look at how we can implement our auxiliary methods if we don't need this.
The general wording:
@using (Html.BeginForm ("Search", "Admin", FormMethod.Post)) { <input type= "text" name= "text"/> < Input type= "Submit" Name= "sub" value= "Search"/>
Do not use HTML helper classes:
@{ var context = this. Viewcontext.requestcontext; Request Context var values = new RouteValueDictionary //Case-insensitive key-value pair collection { {"Controller", "Home"}, {"Action "," Index "} }; var path = RouteTable.Routes.GetVirtualPath (context, values); Returns the Url}<form action= "@path associated with the route. VirtualPath ", method=" POST "> <input type=" text "name=" text "/> <input type=" Submit "name=" Sub " Value= "Search"/></form>
Now we're going to have to write so much code without using a helper class, and we can find that the VS will encapsulate the code we need to write, and it's easy to use.
How the helper method works
Each razor view inherits their base-class HTML attributes, and the type of the HTML attribute is system.web.mvc.htmlhelper<t>; T is a parameter of a generic type that represents the type of model that is passed to the view (by default, dynamic), which provides some instance methods that can be called in the view, and many extension methods (. Come out with a downward arrow to the left of the method name).
: Common Methods of assistance
---: Each helper method has a Htmlattributs property that can set the HTML properties of the page.
---: Most of the helper methods are checking the Viewdate object for the current value to display (all values in the ViewBag object can also be obtained)
---: the helper method can not only view the data inside the ViewData, but also get the model metadata.
When submitting a form, this is typically combined with @using () {} so that resources can be freed. The helper method generates a start tag at the end of the call and returns an object that implements the interface IDisposable. This can be released in the using () {] method.
- Html.validationsummary ()
@Html. ValidationSummary (excludepropertyerrors:true) //Display model-level errors.
This method is used to display an unordered list of all validation errors in the Modelstate dictionary, using a parameter (bool) to tell the level of the secondary method error. (Displays errors related to the model itself and does not display model property-related errors.) )
Public ActionResult Search () { modelstate.addmodelerror ("", "model-level Error"); Set to True modelstate.addmodelerror ("Title", "error at model attribute level"); Set to False return View (); }
- Html.textbox and Html.textarea
are display text, the first is the text box, the second is textarea; TextArea can be set to display the specified number of rows and columns.
Returns a LABEL element and uses a parameter of type string to determine the rendered text and for attribute values. This general use displays the caption of the input box. Displays the display ("name") in the model properties.
[Display (name = "name")] public string Name {get; set;}
- Html.dropdownlist and Html.listbox
Both of these are returned as a <select/> element. DropDownList the first is a one-way selection, and the ListBox is a multiple choice.
With regard to the drop-down list, it requires a collection of SelectListItem objects with all the options available, each of which contains Text,value and selected three properties in the collection of SelectListItem objects. You can build your own collection of SelectListItem objects as needed.
Taking a look at the drop-down used in the previous project, it was not quite understood, and now the reading is to avoid the reflection overhead of generating the SelectListItem collection at the same time, using the LINQ Select method to put the SelectListItem object set into the project.
When an error occurs in a specific field in the Modelstate dictionary, you can use the Validationmessage helper method to display the appropriate error message. The general situation is to display error messages when the server authenticates.
Pass in a hidden way some of the necessary attributes (IDs)--Submit the form.
This is the value of the password box input is hidden. Have not used this before, it seems to use more later.
Radio buttons are generally used in combination to provide a set of options for a user's individual selection.
@Html. RadioButton ("Color", "red", true) @Html. RadioButton ("Color", "blue") @Html. RadioButton ("Color", " Green ")
is the only helper method that renders two input elements. is a hidden domain hidden.
@Html. CheckBox ("checkbox")
Template Assist method
Build HTML with metadata and templates, where metadata includes information about model values (name and type) and model metadata (added through data annotations or custom providers).
You can manipulate some of the values that are bound on the model.
Also set the text box, the good point is that the model can be bound to display the value of HTML.
Auxiliary methods and Modelstate
The user display form is worth all the helper methods also need to interact with the Modelstate dictionary. In fact, Modelstate is a by-product of model binding, and all validation errors detected during model binding are stored, as well as the original values that the user submits to update the model (the validation fails and returns the values previously entered in the view).
Rendering helper Methods
Renders as a hyperlink. This effect is a default route.
This is the rendering part of the path.
@Html. ActionLink ("Link Text", "index") <br/> <a href= "@Url. Action (" index ") >123</a>
The truth is the same as the previous one. Just this can only receive the route name, but not the controller name and operation method
@Html. RouteLink ("Link Text", new{action= "Index"})
URL helper method
This is the same as it was introduced earlier. This only shows part of the path.
The relative path of the user program can be converted to an absolute path. It has not been used in the project yet.
@Url. Content ("~/scripts/_references.js") //~: Can be thought of as the root directory of the application
As before, only the route name is received.
Html.partial and Html.renderpartial
The partial method is used to render a partial view as a string. That is, a partial attempt.
Instead of a string, RenderPartial writes directly to the response output stream. You must put this into a code block when you use it.
@{ html.renderpartial ("List");}
Html.action and Html.renderaction
As in the above, the partial helper method typically applies a view tag in a separate file to help the view render part of the view model, and the action performs a separate controller operation and displays the results. In fact, action is the way to go inside. You can use it when you need to perform some logic.
Renderaction can also be written directly into the response stream. Not many places to use.
@Html. Action ("Search", new{student=new student{age = 12,name = "Andyahui"}})
[Childactiononly] Public ActionResult Search (Student Student) { modelstate.addmodelerror ("", "model-level Error"); Set to True modelstate.addmodelerror ("Title", "error at model attribute level"); Set to False return View (); }
All of the above are some common view helper classes that we can use to do the writing of the foreground part of the code.
I don't work hard, others can't do it.
ASP. NET MVC5----Learn about the @html help classes we use