开发者

Asp.net MVC DisplayTemplate Subfolders

Simple question - is this allowed? if so, how do i do it?

As an example, say i have the following:

namespace Model.Event{
    public class Type1{

        [UIHint("Child")]
        public Child Child{ get; set; }

        [UIHint("AnotherClass")]
        public Model.Event.SubNameSpace.AnotherClass AnotherClass{ get; set; }

        [UIHint("Child")]
        Public Model.Event.SubNameSpace.Child Child2{ get; set; }
    }

    public class Child{
        public string SomeText{ get; set; }
    } 
}

namespace Model.Event.SubNameSpace{
    public class AnotherClass{
        public string 开发者_运维知识库SomeText2{ get; set; }
    }

    public class Child{
        public string SomeText2{ get; set; }
    }
}

and in the Views/Shared I have the following structure:

Views/Shared/DisplayTemplates/Model/Event/Type1.ascx

@Model Model.Event.Type1
    @DisplayFor(x=>x.Child)
    @DisplayFor(x=>x.AnotherClass)
    @DisplayFor(x=.x.Child2)

Views/Shared/DisplayTemplates/Model/Event/Child.ascx

@Model Model.Event.Child
    @DisplayFor(x=>x.SomeText)

Views/Shared/DisplayTemplates/Model/Event/SubNameSpace/AnotherClass.ascx

 @Model Model.Event.SubNameSpace.AnotherClass
    @DisplayFor(x=>x.SomeText2)

Views/Shared/DisplayTemplates/Model/Event/SubNameSpace/Child.ascx

 @Model Model.Event.SubNameSpace.Child
    @DisplayFor(x=>x.SomeText2)

I then create a ViewModel as:

public class MyViewModel(){
    [UIHint("Type1")]
    public Model.Event.Type1 {get; set; }
}

and then in the view I want to say

@Model Model.Event.Type1
    @DisplayFor(x=>x.Child)

I want it to then give me the Type1.ascx and because of the UIHint attributes, give me the child views - does that make sense?


Not really. Look at TemplateHelpers.modeViewPaths. It's hard-coded and static.

However, you can have separate DisplayTemplates per controller.

On further thought, this does go through the view engine, so with spit and duct tape you might be able to make it work. Would be messy, though, since you don't get model metadata there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜