29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

database, for example, which I demonstrated when I created the SportsStore application. I can use the HiddenInput<br />

attribute, which causes the helper to render a hidden input field. You can see how I have applied the HiddenAttribute<br />

to the Person class in Listing 22-11.<br />

Listing 22-11. Using the HiddenInput Attribute in the Person.cs File<br />

using System;<br />

using System.Web.Mvc;<br />

namespace HelperMethods.Models {<br />

public class Person {<br />

[HiddenInput]<br />

public int PersonId { get; set; }<br />

public string FirstName { get; set; }<br />

public string LastName { get; set; }<br />

public DateTime BirthDate { get; set; }<br />

public Address HomeAddress { get; set; }<br />

public bool IsApproved { get; set; }<br />

public Role Role { get; set; }<br />

}<br />

}<br />

// ... other types omitted for brevity ...<br />

When this attribute has been applied, the Html.EditorFor and Html.EditorForModel helpers will render a<br />

read-only view of the decorated property (which is the term used to describe a property to which an attribute has been applied), as<br />

shown in Figure 22-6, which shows the effect of starting the application and navigating to the /Home/CreatePerson<br />

URL.<br />

Figure 22-6. Creating a read-only representation of a property in an editor<br />

The value of the PersonId property is shown, but the user cannot edit it. The HTML that is generated for the property is as<br />

follows:<br />

...<br />

<br />

0<br />

<br />

<br />

...<br />

591

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!