views:

32

answers:

0

I'm writing an MVC2 app using DataAnnotations. I have a following Model:

public class FooModel 
{
    [ScaffoldColumn("false")]
    public long FooId { get; set; }

    [UIHint("BarTemplate")]
    public DateTime? Bar { get; set;}
}

I want to create a custom display template for Bar. I have created following template:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime?>" %>

<div class="display-label">
    <span><%: Html.LabelFor(m => m.Bar) %></span>
</div>
<div class="display-field">
    <span><: Html.DisplayFor(m => m.Bar)%></span>
    <%: Html.ActionLink("Some link", "Action", new { id = ??FooId?? }) %>
</div>

Now, my problem is that inside template for Bar I want to access another property from my model. I don't want to create a separate template for FooModel because than I will have to hardcode all other FooModel properties.

After a brief investigation with a debugger I can see that:

  1. this.ViewData.ModelMetadata.ContainerType is FooModel (as expected)
  2. this.ViewData.TemplateInfo has a non-public property VisitedObjects (of type System.Collections.Generic.HashSet<object>) which contains two elements: FooModel and DateTime?.

How can I get access to my FooModel? I don't want to hack my way around using Reflection.