Asp.Net MVC ModedMetadata Question for HiddenInput

I have a very simple dll presentation model that I want to keep separate from the main web mvc project.

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

Most of the attributes come from the System.ComponentModel.DataAnnotations namespace but I was surprised to find that HiddenInput is an exception and I need to add a link to System.Web.Mvc in my model dll.

Is there any special reason not to include this in other attributes?

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

+4
source share
1 answer

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

However, you can use the [UIHint] attribute:

 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 select a custom template.

Of course, using [HiddenInput] preferable since it forces you to write less code, but if you don't want to reference System.Web.Mvc in your project, you still have a workaround for UIHint .

+3
source

All Articles