Model templates for ASP. net mvc Learning

Source: Internet
Author: User

If you use ASP. net mvc production background will certainly fall in love with its EditorForModal, DisplayForModal and LabelForModal methods, because these methods can directly turn the model into a corresponding tag, saving a lot of trouble, however, for some demanding people, you must customize them. Next we will first introduce how to use them and then introduce how to customize them. Ii. Body 1. for the output model, we first need to create a Home controller, corresponding to an Index action, and an Index view. Then we create an Address class under Modal: copy the Code 1 namespace MvcStudy. models 2 {3 public class Address 4 {5 public string Line1 {get; set;} 6 public string Line2 {get; set;} 7 public string City {get; set ;} 8 public string PostalCode {get; set;} 9 public string Country {get; set;} 10} 11} then create a new Role enumeration: copy the Code 1 namespace MvcStudy. models2 {3 public enum Ro Le4 {5 Admin, 6 User, 7 Guest8} 9} finally creates a Person class: 1 namespace MvcStudy. models 2 {3 public partial class Person 4 {5 public int PersonId {get; set;} 6 public string FirstName {get; set;} 7 public string LastName {get; set ;} 8 public DateTime BirthDate {get; set;} 9 public Address HomeAddress {get; set;} 10 public bool IsApproved {get; set;} 11 public Role {get; set ;} 12} 13} Then we Write the following code into the Index method of the Home Controller: Copy code 1 namespace MvcStudy. controllers 2 {3 public class HomeController: Controller 4 {5 public ActionResult Index () 6 {7 Person p = new Person 8 {9 PersonId = 111 HomeAddress = new Address11 {12 City = "zj", 13 Country = "js", 14 Line1 = ", 15 Line2 = "222", 16 PostalCode = "asdsa" 17}, 18 Role = Models. role. user, 19 BirthDate = DateTime. now, 20 FirstName = "y", 21 LastNa Me = "zf", 22 IsApproved = false23}; 24 return View (p); 25} 26} 27} copy the code and make sure that the model type of the Index View is Person, if not, add the following code at the top: @ model MvcStudy. models. then we open the Index view and write @ Html. editorForModel (). After running, we can see that the attributes of this class are output on the page: Figure 1.1. However, we can find that the PersonId is output and can be edited. This is not what we want, therefore, we need to add the HiddenInput annotation attribute to the model's PersonId. After re-compilation, we can see that the PersonId is no longer Editable: but sometimes we don't want it to be displayed at all, at this time, we need to modify the HiddenInput annotation attribute and set DisplayValue to false. However, you can use Check the html source code of the page to see a hidden input tag. If you do not want this attribute to exist in the page, you can change HiddenInput to [ScaffoldColumn (false. In Figure 1.1, we can find that the label displays the name of this attribute and does not meet the actual usage habits. Therefore, we need to add the name of this attribute to these attributes: copy code 1 namespace MvcStudy. models 2 {3 public partial class Person 4 {5 [ScaffoldColumn (false)] 6 public int PersonId {get; set;} 7 [Display (Name = "Name")] 8 public string FirstName {get; set;} 9 [Display (Name = "surname")] 10 public string LastName {get; set ;} 11 [Display (Name = "Birthday")] 12 public DateTime BirthDate {get; set;} 13 public Ddress HomeAddress {get; set;} 14 [Display (Name = "enable")] 15 public bool IsApproved {get; set ;} 16 [Display (Name = "Role")] 17 public Role {get; set;} 18} 19} after the code is copied and re-compiled, the page is changed to the following: we can see that all strings are rendered using input, but sometimes this does not conform to the actual situation, so we need to modify the DataType type of the attribute. For example, we need to change the last name to Rich Text and modify the Person code: copy code 1 namespace MvcStudy. models 2 {3 public partial class Person 4 {5 [ScaffoldColumn (false)] 6 public int PersonId {get; Set;} 7 [Display (Name = "Name")] 8 [DataType (DataType. text)] 9 public string FirstName {get; set;} 10 [Display (Name = "last Name")] 11 [DataType (DataType. multilineText)] 12 public string LastName {get; set;} 13 [Display (Name = "Birthday")] 14 [DataType (DataType. dateTime)] 15 public DateTime BirthDate {get; set;} 16 public Address HomeAddress {get; set;} 17 [Display (Name = "enable")] 18 public bool IsApproved {get; Set;} 19 [Display (Name = "Role")] 20 public Role {get; set ;} 21} 22} copy the code and then re-compile the code to see the final effect. Of course, these are all ASP. net mvc automatically determines based on the type and selects the corresponding template, so we can also directly set the template used by the attribute, in this way, you can use UIHint to specify the Template Name (actually the view name ). 2. metadata is used for distribution classes. Currently, the website model is automatically generated by tools. If we use the above method to re-generate the model, we need to re-Add the annotation attribute, time-consuming and laborious. Fortunately, ASP. net mvc provides a solution. We only need to add a distribution class for that model class. For example, we will transfer all the annotation attributes of Person to the distribution class below: the author created a class named PersonExt: Copy code 1 namespace MvcStudy. models 2 {3 public class PersonDataBindSource 4 {5 [ScaffoldColumn (false)] 6 public int PersonId {get; set;} 7 [Display (Name = "Name")] 8 [DataType (DataType. text)] 9 public string FirstName {get; set;} 10 [Display (Name = "last Name")] 11 [DataType (DataType. multilineText)] 12 public string LastName {get; set;} 13 [Display (Name = "Birthday")] 14 [DataType (DataType. dateTime)] 15 public DateTime BirthDate {get; set;} 16 public Address HomeAddress {get; set;} 17 [Display (Name = "enable")] 18 public bool IsApproved {get; set;} 19 [Display (Name = "Role")] 20 public Role {get; set ;} 21} 22 23 [MetadataType (typeof (PersonDataBindSource)] 24 public partial class Person25 {26} 27} copy the code and then we can delete Per The annotation attribute of the son class is returned. You can see that the final output is the same after re-compiling. Careful readers will find that the Address attribute is not output, because the Helper does not Recursively search, so we need to write @ Html in the Index view. editorFor (x => x. homeAddress. 3. using a custom template, we can see from the above example that Role presents an input box, but it should actually be selected. In this case, we need to customize the template, first, create an EditorTemplates folder under Views/Shared, and then create a new Role view. The strong type is Role and the master is not used. Then write the following code in the View: 1 <select id = "Role" name = "Role"> 2 @ foreach (Role value in Enum. getValues (typeof (Role) 3 {4 <option value = "@ value" @ (Model = value? "Selected = 'selected'": "")> @ value </option> 5} 6 </select> then refresh the page. We can see that the Role is changed to the drop-down list: of course, the folder name is a standard. If you need to customize the Display template, create a DisplayTemplates folder, and then create a view with the corresponding name based on the type. Of course, you can also create some ASP. net mvc already provides the template type, while ASP. net mvc will first use our template instead of its own. Of course, you can also specify a template through UIHint. Sometimes we need to set the tag Id, then we can use ViewData. templateInfo. the GetFullHtmlFieldId method obtains the Id and ViewData. templateInfo. getFullHtmlFieldName is used to obtain the name. If you are not satisfied with this, you can use ViewData. the attributes in ModelMetadata obtain all the attributes in the model. 4. Pass parameters to the template. Sometimes we need to pass custom parameters between the model template and the template. The following method can be implemented. First, add the following annotation attributes to the BirthDate attribute in the PersonExt file: the first parameter of the annotation attribute is the Key, and the second parameter is the corresponding parameter, create a DateTime view in Views/Shared/EditorTemplates and write the following code: Copy code 1 @ model DateTime 2 3 @ {4 Layout = null; 5} 6 7 @ Html. textBox (ViewData. templateInfo. getFullHtmlFieldName ("a"), Model) 8 @ if (ViewData. modelMetadata. additionalValues. containsKey ("val") 9 {10 @ Html. label (ViewData. modelMetadata. additionalValues ["val"]. toString () 11}

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.