views:

24

answers:

1

I have a very simple view-model in a dll that I want to keep separated from the main web mvc project.

I am decorating the model with metadata attributes that will help the ui display the correct presentation (DisplayName, UIHint, DataType, ReadOnly etc) and I would like to reuse this information with different presentation layers later (like Silverlight)

Most of the attributes are coming from the namespace System.ComponentModel.DataAnnotations but I was surprised to discover that HiddenInput is an exception to that and I need to add a reference to System.Web.Mvc in my view-model dll.

Is there a particular reason not to have included that with the other attributes?

I tried to override the default behavior putting an HiddenInput.ascx in the editortemplates folder but I still get the label for the field when I call an html.EditorfForModel() in my view.

+1  A: 

I believe the reason for not being included in the System.ComponentModel.DataAnnotations is because this assembly is part of the BCL and existed before ASP.NET MVC. Brad Wilson wrote a nice blog post covering model metadata in MVC which you might read.

This being said you could use the [UIHint] attribute like this:

public class MyViewModel
{
    [UIHint("Hidden")]
    public string Value { get; set; }
}

And in ~/Views/Home/EditorTemplates/Hidden.ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%: Html.HiddenFor(x => x) %>

Now you can use <%= Html.EditorForModel() %> in your view and it will pick the custom template.

Of course using [HiddenInput] is preferred as it makes you write less code but in case you don't want to reference System.Web.Mvc in your project you still have the UIHint workaround..

Darin Dimitrov