public class YearMonthValueViewModel<TValue>
{
public List<MonthValueViewModel<TValue>> Months { get; set; }
}
public class MonthValueViewModel<TValue>
{
public MonthEnum Month { get; set; }
public TValue Value { get; set; }
}
I’m wanting to create a partial view that takes either YearMonthValueViewModel<int> or YearMonthValueViewModel<double>. How do I do this without creating two partial views with exactly the same code? Here’s the view’s code for reference:
<% for(var j = 0; j < Model.Months.Count; j++) { %>
<div>
<%: Model.Months[j].Month.ToString() %><br />
<%: Html.TextBoxFor(model => model.Months[j].Value)%>
<%: Html.CustomValidationMessageFor(model => model.Months[j].Value)%>
</div>
<% } %>
You can pass your object using ViewData, or pass it as <Object> etc, and check the object type in your code within the partial view.
It is not generally recommended to so thou.
You should try to prepare your presentation information in your controller.
But I do this sometimes as well.
Updated:
I think there are many ways to do this,
for example, you could, as suggested by the comment, prepare it into a List by a helper method in your controller.
or override your YearMonthValueViewModel.toString() method