MVC2 - How to obtain parent model (container) inside template
Asked Answered
U

5

6

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.LabelForModel() %></span>
</div>
<div class="display-field">
    <span><%: Html.DisplayForModel()%></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.

Update:

I've accepted mootinator's answer as it looks to me as the best solution that allows type-safety. I've also upvoted Tx3's answer, as mootinator's answer builds upon it. Nevertheless, I think that there should be a better support form MVC in those kind of scenarios, which I believe are quite common in real world but missing from sample apps.

Uphemia answered 24/10, 2010 at 16:27 Comment(6)
@Jakub: The model of Bar.cshtml is type of DateTime?, there is no m.Bar I think.Oldtimer
@Recycle Bin - Cheers, edited the question.Uphemia
@Jakub: I don't understand why you need to access FooModel from within DateTime?. It does not make sense. :-)Oldtimer
@Recycle Bin - imagine I have a UserDetailsModel that has a DateTime? property called LastLoginDate. I want to create a template for this datetime property that will be used by EditorForModel() to render a date time picker and a link to login history page, for which I need UserId.Uphemia
@Jakub: The Creative Commons license that Wikipedia uses requires citations. Please review your tag wiki edits, and add the needed citation to each. See here for an example: stackoverflow.com/tags/smtp/info. Note the Wikipedia link I've added to the bottom.Suzansuzann
@Robert - Thank you, will do thatUphemia
S
0

Sorry if this suggestion seems daft, I haven't tried it, but couldn't you do what Tx3 suggested without having to create a bunch of new classes by defining a generic class to reference whatever type of parent you want?

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

        [UIHint("BarTemplate")]
        public ParentedDateTime<FooModel> Bar { get; set;}

        public FooModel()
        {
            Bar = new ParentedDateTime<FooModel>(this);
        }
    }


    public class ParentedDateTime<T>
    {
        public T Parent {get; set;}
        public DateTime? Babar {get; set; }

        public ParentedDateTime(T parent)
        {
            Parent = parent;
        }

}

You could expand that to encapsulate any old type with a <Parent, Child> typed generic, even.

That would also give you the benefit that your strongly typed template would be for

Inherits="System.Web.Mvc.ViewUserControl<ParentedDateTime<FooType>> thus you would not have to explicity name which template to use anywhere. This is more how things are intended to work.

Salvucci answered 21/2, 2011 at 18:46 Comment(3)
You could go a step further and create ParentedField<TParent, TField>. You would need 1 class but still your model would be 'ugly' as FooModel properties will be of type ParentedField<FooModel, DateTime?> etc. It still smells for me. My point is that ModelMetadata already has my parent object - I don't want to 'polute' my model (image how it would look like when serialized to JSON, which I also do).Uphemia
@Jakub I would probably just keep a separate ViewModel for JSON. I can see why you'd want to avoid the duplication, if possible.Salvucci
The syntax which specifically exists to accomplish what you want still involves using ViewData: dalsoft.co.uk/blog/index.php/2010/07/29/…Salvucci
R
4

Maybe you could create new class, let's say UserDateTime and it would contain nullable DateTime and rest of the information you need. Then you would use custom display template for UserDateTime and get access to information you require.

I realize that you might be looking for other kind of solution.

Reverberator answered 15/2, 2011 at 13:16 Comment(2)
This would mean changing my UserDetailsModel, which I would rather not do. Should I have 5 DateTime properties I would have to keep creating different classes and copy data around. My point is: I already have data I need in my Model. Thanks anyway!Uphemia
Good points. I am also interested to hear what is the solution.Reverberator
E
2

I think you may be better off extracting this functionality to an HtmlHelper call from the Parent View.

Something like RenderSpecialDateTime<TModel>(this HtmlHelper html, Expression<Func<TModel,DateTime?>> getPropertyExpression) would probably do the job.

Otherwise, you will have to do something like what Tx3 suggested. I upvoted his answer, but posted this as an alternative.

Erubescence answered 18/2, 2011 at 2:24 Comment(1)
Sorry, I don't get it. 1) Extract what functionality? 2) How am I supposed to call Html.RenderSpecialDateTime() in the Parent View when all I'm doing in the Parent View is Html.DisplayForModel()?Uphemia
R
1

Couldn't you use the ViewData dictionary object in the controller and then grab that in the ViewUserControl? It wouldn't be strongly typed but...you could write a helper to do nothing if it's empty, and link to say the example login history page if it had a value.

Ramiform answered 15/2, 2011 at 22:26 Comment(5)
I don't want to duplicate the data. I already have it in my model and I don't want to start putting half of my model into ViewData.Uphemia
Instead of trying to mix and match, stuff the model into the ViewData and then pull it out in the control.Ramiform
ViewData is not type safe. I value type-safety a lot.Uphemia
Don't understand the vote down. While not the solution Jakub was looking for, but it's not an incorrect solution. Not everyone values type-safety in the same way. :)Ramiform
Didn't intend to imply that. Shrug. :)Ramiform
A
1

It would appear that somewhere between MVC 5.0 and 5.2.2 a "Container" property was added on to the ModelMetadata class.

However, because all of the methods in a provider responsible for metadata creation (GetMetadataForProperty, Create etc) do not have container in their signature, the Container property is assigned only in certain cases (GetMetadataForProperties and GetMetadataFromProvider according to reflected code) and in my case was usually null.

So what I ended up doing is overriding the GetMetadataForProperty in a new metadata provider and setting it there:

public override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, string propertyName)
{
  var propMetaData = base.GetMetadataForProperty(modelAccessor, containerType, propertyName);
  Object container = modelAccessor.Target.GetType().GetField("container").GetValue(modelAccessor.Target);
  propMetaData.Container = container;
  return propMetaData;
}

I know this is reflection but it's fairly succinct. It would appear that MS is correcting this oversite so maybe it will be possible to replace the reflection code in the future.

Amazement answered 20/1, 2015 at 23:57 Comment(0)
S
0

Sorry if this suggestion seems daft, I haven't tried it, but couldn't you do what Tx3 suggested without having to create a bunch of new classes by defining a generic class to reference whatever type of parent you want?

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

        [UIHint("BarTemplate")]
        public ParentedDateTime<FooModel> Bar { get; set;}

        public FooModel()
        {
            Bar = new ParentedDateTime<FooModel>(this);
        }
    }


    public class ParentedDateTime<T>
    {
        public T Parent {get; set;}
        public DateTime? Babar {get; set; }

        public ParentedDateTime(T parent)
        {
            Parent = parent;
        }

}

You could expand that to encapsulate any old type with a <Parent, Child> typed generic, even.

That would also give you the benefit that your strongly typed template would be for

Inherits="System.Web.Mvc.ViewUserControl<ParentedDateTime<FooType>> thus you would not have to explicity name which template to use anywhere. This is more how things are intended to work.

Salvucci answered 21/2, 2011 at 18:46 Comment(3)
You could go a step further and create ParentedField<TParent, TField>. You would need 1 class but still your model would be 'ugly' as FooModel properties will be of type ParentedField<FooModel, DateTime?> etc. It still smells for me. My point is that ModelMetadata already has my parent object - I don't want to 'polute' my model (image how it would look like when serialized to JSON, which I also do).Uphemia
@Jakub I would probably just keep a separate ViewModel for JSON. I can see why you'd want to avoid the duplication, if possible.Salvucci
The syntax which specifically exists to accomplish what you want still involves using ViewData: dalsoft.co.uk/blog/index.php/2010/07/29/…Salvucci

© 2022 - 2024 — McMap. All rights reserved.