tags:

views:

36

answers:

3

OK, I'm pretty sure I know the answer to this but just in case.

I know I can pass in a composite class to a view / partial view. My question though is can I pass in an object without first having a model.

So something like Html.RenderPartial("MyPartialView", new { id=10, name="slappy" });

If I can, what would the partial view look like?

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<?????>" %>

and how would I access the properties?

Model.id???

EDIT

Just a follow up based on @Graphain's solution this is in my view;

<% Html.RenderPartial("PagedList", new { id="10" } ); %>

Within my Partial View I have this;

[<%= ViewData["id"] %>] which renders [];

However this;

<%= Html.TextBox("id") %>

Gives me a textbox with the number 10 in it.

+3  A: 

Sure.

Your partial view declaration should look like this:

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 

And you'll access properties like this:

 <%=ViewData.Eval("id")%>

You'll be using ViewUserControl rather than ViewUserControl(Of TModel).

This means you'll be accessing a ViewDataDictionary rather than a ViewDataDictionary(Of TModel) and will be dealing with just regular object data rather than strongly typed data (as well as string keys rather than strong-typed property names).

This link is a good intro on the differences:

The first type of ViewDataDictionary is an untyped collection consisting of a string and an object. The string represents the key, and the object holds the actual data. Like any untyped collection, the object can be of any type, but must be cast to the correct type before it is used. Items are referenced via their key in the ViewData property of the ViewUserControl (in the case of a PartialView). The second is a strongly typed collection, where items are properties of the ViewUserControl.Model property.

Graphain
The view data is not present in my partial view.
griegs
So if you go ViewData["id"] or ViewData["slappy"] nothing appears?
Graphain
Yeah that is correct. I have this in my view <% Html.RenderPartial("PagedList", new { id="10" }); %> and in my PV I have [<%= ViewData["id"]%>] and all I see is []
griegs
I'm looking into it but can you try <%=ViewData.Count%> for me?
Graphain
Also <%=ViewData.Values.First()%> ?
Graphain
That actually gets me the first one but I already have another ViewData there which is the application title.
griegs
This [<%= ViewData.Values.ToList()[0] %>] also works but [<%= ViewData.Values.ToList()[1] %>] does not as it is past the size of the list array
griegs
How can you already have another ViewData there? Is the partial somehow gaining access to its parent's ViewData?
Graphain
From a base controller class which grabs the application name from a config file and places that into the view data "applicationname" which is rendered in the title of the master page and other places.
griegs
Try: ViewData.Eval("Id"). There was a change I was unaware of (I stopped using untyped ViewData).
Graphain
Hahaha oddly enough because of this same problem: (http://stackoverflow.com/questions/61805/asp-net-mvc-viewdata-using-indices-question/61808#61808).
Graphain
Partials shouldn't be able to access that parent ViewData though.
Graphain
@Graphain, Eval worked thanks. Could you please update your answer so other don't have to trawl through all our comments and then I'll mark your's as the answer. Thanks for you help and patience.
griegs
Yeah just did that. No problem, the doc should be clearer.
Graphain
A: 

I don't think you can use an Anonymous class as ViewData.

Craig
Yeah that's what I'm finding. The code from @Graphain works right up until I run it. :) ViewData is not coming across.
griegs
You can definitely access that anonymous type's properties using ViewData by using the string-based indexer, ViewData["someProperty"]. This is what Graphain's post shows.
Eilon
A: 
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ExpandoObject>" %>

In your controller:

var model = new ExpandoObject();
model.id = 1;
model.name = "John";
model.DynamicAttrs = "anything dynamic";

Maybe you are confused that why don't we use:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>

which theoretically gives a Model whose type is dynamic. But here we can't because dynamic types are generated as "internal" that you can't visit it in the View. So we use ExpandoObject instead.

Danny Chen