0

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">&laquo;</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">&raquo;</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.

Juzzbott
  • 1,737
  • 2
  • 25
  • 44
  • What is the model in `Pagination.cshtml` - i.e `@model ???` –  Jun 01 '15 at 07:51
  • If your `@model` is `object` you should change to `dynamic`. – adricadar Jun 01 '15 at 07:53
  • i'v read a post before,anonymous type will compile to a internal class,so you can't use it in cshtml.the post is chinese,you can read if you want. http://blog.zhaojie.me/2011/09/aspnet-mvc-dynamic-model-mono-cecil.html – chenZ Jun 01 '15 at 07:56
  • 1
    Anonymous objects are internal - refer [this article](http://www.gregshackles.com/anonymous-view-models-in-asp-net-mvc-using-dynamics/) for an explanation –  Jun 01 '15 at 07:58
  • I'm not declaring any model... Adding `@model dynamic` has no effect. – Juzzbott Jun 01 '15 at 08:12
  • So because each cshtml file is compiled to it's own DLL file, internal types cannot be shared? Is there any way to compile all views as a single DLL? I really don't want to have to be setting referenced to front end code (i.e. div ids and javascript function names) in my controller code... – Juzzbott Jun 01 '15 at 08:14
  • Did you try to use ViewBag to pass Model.UserPlaces ? – V-SHY Jun 01 '15 at 08:19
  • 2
    I don't see why you'd want to do this. You can create variables at run time in your view anyway... having a view model makes it clear what it being sent from the controller and what is required for the view. – Luke Jun 01 '15 at 08:26

0 Answers0