ASP.net MVC - rendering a List containing different types, with a different view for each type

后端 未结 5 1056
清歌不尽
清歌不尽 2020-12-29 00:29

Imagine I have a list of objects that implement an interface called ISummary The objects within this list MAY have additional properties ie.

public interface         


        
5条回答
  •  抹茶落季
    2020-12-29 00:47

    You could put a helper method in the view's codebehind, and then do something like:

    Type modelType = this.Model.GetType();
    
    if (modelType == typeof(NewsSummary))  this.RenderPartial("newspartial", this.Model as NewsSummary);
    else if (modelType == typeof(GigSummary)) this.RenderPartial("gigpartial", this.Model as GigSummary);
    

提交回复
热议问题