We have a ViewModel with a collection of Widget<dynamic>:
public class Widget<T>
{
public string ViewName { get; set; }
public T Data { get; set; }
}
In our View we loop through the collection of widgets and call Html.Partial.
@foreach (var widget in Model) {
@Html.Partial(widget.ViewName, widget)
}
This works providing the partial has a dynamic model or a model of Widget<dynamic>.
However, I would like intellisense within the partial view so I give my view a model of the appropriate widget data type e.g:
@model Widget<string>
Unfortunately this throws an exception as @Html.Partial is casting the dynamic model as System.Object.
Aside from manually casting the widget again within the partial, is there a way to call Html.Partial without having the dynamic model cast as an object?
Create a interface and all dynamic object type widgets must implements the interface, I did the same a time ago and worked smooth
Simple example with render: (it’s just a quick example you must complete)
the view….