The ASP. NET MVC5 Framework is an open-source, extensible framework everywhere.
In his book, Mr. Jiang gives a lot of explanations and examples of how to understand the framework and how to extend the framework.
First on
Most of the students who do traditional BS see this page, the first reaction in the brain is a bunch of HTML a bunch of controls and then back-bound something.
But take a look at the code of the page.
- @model MvcApp.Models.Employee
- <html>
- <head>
- <title> Edit employee Information </title>
- </head>
- <body>
- <table>
- <tr>
- <TD>@Html. labelfor (M = m.name)</td>
- <TD>
- @Html. editorfor (M = m.name)
- </td>
- </tr>
- <tr>
- <TD>@Html. labelfor (M = m.gender)</td>
- <TD>
- @Html. editorfor (M = m.gender)
- </td>
- </tr>
- <tr>
- <TD>@Html. labelfor (M = m.education)</td>
- <TD>
- @Html. editorfor (M = m.education)
- </td>
- </tr>
- <tr>
- <TD>@Html. labelfor (M = m.departments) </td>
- <TD>
- @Html. editorfor (M = m.departments)
- </td>
- </tr>
- <tr>
- <TD>@Html. labelfor (M = m.skills)</td>
- <TD>
- @Html. editorfor (M = m.skills)
- </td>
- </tr>
- </Table>
- </body>
- </html>
How to do it? What is this MvcApp.Models.Employee? Why does it magically become a bunch of different list controls?
- namespace Mvcapp.models
- {
- Public class Employee
- {
- [DisplayName (" name ")]
- Public string Name { get; set; }
- [RadioButtonList ("Gender")]
- [DisplayName (" gender ")]
- Public string Gender { get; set; }
- [DropdownList ("Education")]
- [DisplayName (" Education ")]
- Public string Education { get; set; }
- [ListBox ("Department")]
- [DisplayName (" Department ")]
- Public ienumerable<String> Departments { get; set; }
- [CheckBoxList ("Skill")]
- [DisplayName (" Good Skills ")]
- Public ienumerable<string> Skills { get; set; }
- }
- }
See here for C # and. NET are familiar with the students must be a bit of a mental spectrum, features! Oh, it's all reflected through the features. And how does that reflect?
The MVC framework first determines the sub-templates for each feature through Templatehint, which reflects the past through the mapping of the names entirely.
And then in the template through code @ html . dropdownlist (
To invoke the custom pair dropdownlist Then in this extension method to obtain the required list of data ienumerable<listitem > , and then based on this list of data to build the required List <selectlistitem
Finally, we call the existing htmlhelper. DropDownList (name, selectlistitems); returned the required mvchtmlstring .
The entire extension is complete.
At the same time there are a lot of doubts to dig into.
Like @Html. DropDownList ("", ListName, Model) The first parameter of this code, in the end what role? Why is it a null value?
Through the actual test I found that the HTML generated in the case of NULL and non-NULL has the following differences.
- "" Parameter
- <tr>
- <td><label for = "education" > Education </label></td>
- <td>
- <select id= "education" name= "education" ><option value= "H" > High School </option>
- <option Value= "B" > University degree </option>
- <option selected= "selected" value= "M" > Master </option>
- <option Value= "D" > PhD </option>
- </select>
- </td>
- </tr>
- The "Test" parameter
- <tr>
- <td><label for = "education" > Education </label></td>
- <td>
- <select id= "education_test" name= "education.test" ><option value= "H" > High School </option>
- <option Value= "B" > University degree </option>
- <option selected= "selected" value= "M" > Master </option>
- <option Value= "D" > PhD </option>
- </select>
- </td>
- </tr>
Why is there a difference, please study the code to read it.
Do you really play out the power of the ASP. NET MVC framework?
ASP. NET MVC5 Framework uncover S412 instance resolution – The triumph of the wonderful extended mode