ASP. NET MVC5 Framework uncover S412 instance resolution – The triumph of the wonderful extended mode

Source: Internet
Author: User

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.

  1. @model MvcApp.Models.Employee
  2. <html>
  3. <head>
  4. <title> Edit employee Information </title>
  5. </head>
  6. <body>
  7. <table>
  8. <tr>
  9. <TD>@Html. labelfor (M = m.name)</td>
  10. <TD>
  11. @Html. editorfor (M = m.name)
  12. </td>
  13. </tr>
  14. <tr>
  15. <TD>@Html. labelfor (M = m.gender)</td>
  16. <TD>
  17. @Html. editorfor (M = m.gender)
  18. </td>
  19. </tr>
  20. <tr>
  21. <TD>@Html. labelfor (M = m.education)</td>
  22. <TD>
  23. @Html. editorfor (M = m.education)
  24. </td>
  25. </tr>
  26. <tr>
  27. <TD>@Html. labelfor (M = m.departments) </td>
  28. <TD>
  29. @Html. editorfor (M = m.departments)
  30. </td>
  31. </tr>
  32. <tr>
  33. <TD>@Html. labelfor (M = m.skills)</td>
  34. <TD>
  35. @Html. editorfor (M = m.skills)
  36. </td>
  37. </tr>
  38. </Table>
  39. </body>
  40. </html>

How to do it? What is this MvcApp.Models.Employee? Why does it magically become a bunch of different list controls?

  1. namespace Mvcapp.models
  2. {
  3. Public class Employee
  4. {
  5. [DisplayName (" name ")]
  6. Public string Name { get; set; }
  7. [RadioButtonList ("Gender")]
  8. [DisplayName (" gender ")]
  9. Public string Gender { get; set; }
  10. [DropdownList ("Education")]
  11. [DisplayName (" Education ")]
  12. Public string Education { get; set; }
  13. [ListBox ("Department")]
  14. [DisplayName (" Department ")]
  15. Public ienumerable<String> Departments { get; set; }
  16. [CheckBoxList ("Skill")]
  17. [DisplayName (" Good Skills ")]
  18. Public ienumerable<string> Skills { get; set; }
  19. }
  20. }

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.

  1. "" Parameter
  2. <tr>
  3. <td><label for = "education" > Education </label></td>
  4. <td>
  5. <select id= "education" name= "education" ><option value= "H" > High School </option>
  6. <option Value= "B" > University degree </option>
  7. <option selected= "selected" value= "M" > Master </option>
  8. <option Value= "D" > PhD </option>
  9. </select>
  10. </td>
  11. </tr>
  12. The "Test" parameter
  13. <tr>
  14. <td><label for = "education" > Education </label></td>
  15. <td>
  16. <select id= "education_test" name= "education.test" ><option value= "H" > High School </option>
  17. <option Value= "B" > University degree </option>
  18. <option selected= "selected" value= "M" > Master </option>
  19. <option Value= "D" > PhD </option>
  20. </select>
  21. </td>
  22. </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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.