Html.EditorFor in ASP.NET MVC 2.0

I have mainly been focusing on the Html.DisplayFor and Html.LabelFor in ASP.NET MVC 2.0. However, as of Preview 1, we also have an Html.EditorForTemplate Helper that is used specifically for editing data in your view pages. Focusing on the Contact Manager example that I have been discussing in these tutorials, if in the Edit ViewPage I write the following line of code where Contact is the strongly-typed ViewModel:

<%= Html.EditorFor(contact => contact) %>

By default the Html.EditorFor Template Helper will use reflection to get the properties of Contact and display the list of properties as follows:

Html.EditorFor_MVC 

Now just like with Html.DisplayFor, you can create a custom template with the same name as the type in a special folder, called EditorTemplates, to modify the output to your needs:

 

Html.EditorFor_IT_02

 

My custom Contact.ascx template is simply as follows:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ContactManager.Models.Contact>" %>
<p>
    <%= Html.LabelFor(c => c.Name) %>:
    <%= Html.DisplayFor(c => c.Name) %>
</p>
<p>
    <%= Html.LabelFor(c => c.Email) %>:
    <%= Html.DisplayFor(c => c.Email) %>
</p>

Now that the custom template exists and the Html.EditorFor Template Helper is able to find it, it will use the custom template ( rather than reflection ) to display the contact to be edited:

Html.EditorFor_WEB_03

 

ScaffoldColumnAttribute

If we take a quick peek at the two different displays shown above, the main difference is that in my custom template I did not want the primary key ( Id ) to be shown in the form. Seems like such a waste of time and energy to create a custom template to just not display one property. Is there a better way?

Well, there might be depending on the needs and complexity of your ASP.NET MVC Application. The obvious way is to change the visibility or the way I handle primary keys in my application so that they are not displayed. If I did that, however, I wouldn't be able to talk about another possible solution :)

As another possible solution I could use the ScaffoldColumnAttribute, which is in System.ComponentModel.DataAnnotations, to tell the Template Helper that I do not want it to scaffold ( display ) the Id Property. If I add the ScaffoldColumnAttribute to the Id Property ( where false says I do not wish to scaffold it ) :

[ScaffoldColumn(false)]

public long Id { getset; }

the Id Property will not be displayed and I essentially get the same out as my custom template without having to make a custom template:

Html.EditorFor_MVC_04

 

For my simple presentation code the ScaffoldColumnAttribute will work quite nicely :)

As a side note, please don't take my series of tutorials as best practices or recommendations on how one should or should not do things. The key is knowing that there are alternative solutions and they all have their pros and cons. The key is understanding the alternatives and deciding the best solution for yourself based on your situation.

Hope this helps,

David Hayden