I’m trying to pass a ViewModel property to a partial view, but getting the following error:
“The model item passed into the dictionary is of type ‘<>f__AnonymousType2`1[DomaniOnline.Models.DomaniData.TempRates]’, but this dictionary requires a model item of type ‘DomaniOnline.Models.DomaniData.TempRates’.”
How do I pass the VM property so that it is not an anonymous type?
The View:
@model DomaniOnline.Models.ViewModels.CompareRatesViewModel
@{
ViewBag.Title = "Rate Comparison";
}
<h2>Compare Rates</h2>
<table>
<tr>
<td>@Html.DisplayTextFor(m=>m.TempRate1.CarrierName)</td>
<td>@Html.DisplayTextFor(m=>m.TempRate2.CarrierName)</td>
<td>@Html.DisplayTextFor(m=>m.TempRate3.CarrierName)</td>
<td>@Html.DisplayTextFor(m=>m.TempRate4.CarrierName)</td>
</tr>
<tr>
<td>@Html.Partial("_TempRatesPartial", new { tempRate = Model.TempRate1 })</td>
<td>@Html.Partial("_TempRatesPartial", new { tempRate = Model.TempRate2 })</td>
<td>@Html.Partial("_TempRatesPartial", new { tempRate = Model.TempRate3 })</td>
<td>@Html.Partial("_TempRatesPartial", new { tempRate = Model.TempRate4 })</td>
</tr>
</table>
The Partial View:
@model DomaniOnline.Models.DomaniData.TempRates
<fieldset>
<legend>TempRates</legend>
<div class="display-label">Carrier Name</div>
<div class="display-field">
@Html.DisplayFor(model => model.CarrierName)
</div>
....
</fieldset>
And the ViewModel:
public class CompareRatesViewModel
{
public TempRates TempRate1 { get; set; }
public TempRates TempRate2 { get; set; }
public TempRates TempRate3 { get; set; }
public TempRates TempRate4 { get; set; }
public TempRates TempRate5 { get; set; }
public CompareRatesViewModel(IEnumerable<TempRates> TempRateList)
{
this.TempRate1 = TempRateList[0];
this.TempRate2 = TempRateList[1];
this.TempRate3 = TempRateList[2];
this.TempRate4 = TempRateList[3];
this.TempRate5 = TempRateList[4];
}
}
You need to cast your anonymous type as the type that is the model of your partial view: