Model metadata and model template for asp.net MVC: Introducing "ListControl" into asp.net mvc

Source: Internet
Author: User
Tags visual studio

Not only can we create the appropriate template to control the type of data based on the model metadata in the UI interface rendering methods, but also through some extensions to control the model metadata itself. In some cases, the combination of the two can often solve a lot of special data rendering problems, the example that we will demonstrate next is a typical example.

The traditional asp.net has a set of important control types called list controls (ListControl), and its subclasses include DropDownList, ListBox, RadioButtonList, and CheckBoxList. For ASP.net mvc, we can dropdownlist/dropdownlistfor and listbox/through htmlhelper/htmlhelper<tmodel> extension methods The listbox renders a Drop-down box and a list box on the interface, but we need to manually specify all of the list options included. In general Web applications, especially in enterprise applications, we choose to maintain these lists individually, and if we can eliminate the task of manually providing lists when building a "list control", this will undoubtedly lead to great traversal of development, which is actually easy to implement.

This article supporting source code: http://www.bianceng.net/dotnet/201211/644.htm

First, the effect of implementation

Let's take a look at the effect of the final implementation through this extension. In an empty Web application created through the ASP.net MVC project template for Visual Studio, we define an employee type that represents the employees as model. As shown in the following code snippet, attributes that represent gender, education, department, and skills are applied to Radiobuttonlistattribute, Dropdownlistattribute, Listboxattribute and Checkboxlistattribubte four features. As you can see from the name, these four features represent the form that the target element renders on the UI interface, which corresponds to the four types of list controls in the traditional ASP.net Web application: RadioButtonList, DropdownList, ListBox and CheckBoxList. The string specified in the attribute represents the name of the predefined list.

   1:public class Employee
2: {
3: [DisplayName ("name")]
4: Public string Name {get; set;}
5:
6: [RadioButtonList ("Gender")]
7: [DisplayName ("gender")]
8: Public string Gender {get; set;}
9:
[DropdownList ("Education")]
One: [DisplayName ("Education")]
: Public string Education {get; set;}
13:
[ListBox ("Department")]
: [DisplayName ("department")]
: Public ienumerable<string> Departments {get; set;}
17:
[CheckBoxList ("Skill")]
: [DisplayName ("good skill")]
Public ienumerable<string> Skills {get; set;}
21:}

In the default HomeController created, we define one of the following index action methods. In this method, we create a specific employee object and set all of its properties accordingly, and eventually render the object in the default view.

   1:public class Homecontroller:controller
2: {
3: Public actionresult Index ()
4: {
5: Employee employee = new Employee
6: {
7: Name = "John",
8: Gender = "M",
9: Education = "M",
departments= New string[] {"HR", "AD"},
One: Skills = new string[] {"CSharp", "ADONet"}
: };
: Return View (employee);
: }
15:}

As shown below is the view definition corresponding to the index operation above, which is a strongly typed view with model type as employee, we call htmlhelper<tmodel> Template method editorfor All attributes of the employee object as model will be rendered in edit mode.

 1: @model Employee 
2: <table>
3: <tr>
4: <td> @Html. labelfor (M =&G T M.name) </td><td> @Html. Editorfor (M => m.name) </td>
5: </tr>
6: <tr>< br> 7: <td> @Html. labelfor (M => m.gender) </td><td> @Html editorfor (M => m.gender) </TD&G T
8: </tr>
9: <tr>
: <td> @Html. labelfor (M => m.education) </td& Gt;<td> @Html. Editorfor (M => m.education) </td>
One: </tr>
: <tr>
13: <td> @Html. Labelfor (M => m.departments) </td><td> @Html. Editorfor (M => m.departments) </td& Gt Lt;td> @Html. Editorfor (M => m.skills) </td>
: </tr>
: </table>

The following illustration shows the effect of the Web application runtime. As we can see, the four properties are rendered in four different list controls, and the list attributes defined for the four words on them (Radiobuttonlistattribute, Dropdownlistattribute, Listboxattribute and Checkboxlistattribubte).

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.