I'm trying to create an anonymous type in a view file to pass to a partial view, however I'm getting the following error:
'object' does not contain a definition for 'ResultSet'
I'm trying to pass an anonymous object as the model to a view like so:
@Html.Partial("~/Views/Shared/Components/Pagination.cshtml", new { ResultSet = Model.UserPlaces });
I've had a bit of a search around, and some answers have mentioned you cannot create anonymous types in a controller and pass to a view (https://stackoverflow.com/a/7652765/495328) however I'm not creating the anonymous type in a controller in this example.
Also, other SO questions seem to indicate that this issue appears because of an error in another view, and not related to the anonymous type at all. However I don't believe this to be the issue in this case, as this was working fine when the model passed to the view is just Model.UserPlaces
not new { ResultSet = Model.UserPlaces }
My Pagination.cshtml view file looks like:
@if (Model.ResultSet.TotalResults > Model.ResultSet.Limit)
{
<nav>
<ul class="pagination" data-base-url="@Model.ResultSet.BasePageUrl">
<li>
<a href="#" onclick="tracklas.pagination.loadResults('prev', this); return false;" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
@for (int i = 1; i < ((Model.ResultSet.TotalResults / Model.ResultSet.Limit) + 1); i++)
{
<li class="@(i == 1 ? "active" : "")" data-page-num="@i"><a href="#" onclick="tracklas.pagination.loadResults('@i)', this); return false;">@i</a></li>
}
<li>
<a href="#" onclick="tracklas.pagination.loadResults('next', this); return false;" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
}
Interestingly, if I put a break-point on the the first line in the Pagination.cshtml file, I can inspect the 'Model' object in the watch window. As soon as I try to do Model.ResultSet
I get the following message in the Watch window:
The expression cannot be evaluated. A common cause of this error is attempting to pass a lambda into a delegate.
The reason I'm trying to accomplish this is to expand the anonymous type to include front-end references (elements ids, etc) and I want to specify these in the view, rather than in the controller with a strongly typed model object. I figured this is exactly why anonymous types were created/is useful for.