c# - Using a Model Class of Type T or Interface for Strongly Typed View -
i'm trying refactor view , model. previously, typed view had model directive in cshtml file:
@model myviewmodel<mysubviewmodel>
i've since identified type mysubviewmodel needs either generic or inheritable because i've identified multiple types should interchangeable here.
first tried this:
@model myviewmodel<t>
... couldn't make work, so, wishfully thinking, tried this:
@model myviewmodel<t> t : imysubviewmodel
... that's no either, tried this:
@model myviewmodel<imysubviewmodel>
... @ first thought working visual studio didn't underline it. however, saw every place have razor insertion, it's showing error message "the name 'whatever' not exist in current context." goes viewbag, html helpers , usages of view model.
so how should done? or can not done? realize use:
@model myviewmodel<dynamic>
... i'd rather not lose intellisense given know "dynamic" type implements imysubviewmodel.
this question similar these questions:
why can't view's model bind generic viewmodel implements interface? (asp.net mvc 3) pass subclass of generic model razor view
so, drawing those, need introduce interface covariant generic type parameter:
Comments
Post a Comment