views:

129

answers:

2

I've got some data in a view that I would like to pass to a child partial view. Part of that data is a list of dates that I would like to display in the partial view. I'm pretty sure I can't pass an IEnumerable from one view to another (when I try list is null in the controller). Assuming that is the case, is there a good work around?

I've thought about just concatenating the values into a string and then just parsing that string in the controller. That seems a bit hackish, but I think it would work. Is there a problem with doing it like that? Is there a better way?

It just seems like such a shame to have to re-fetch the data that I've got in the parent view. I'm hoping there's another way to do it.

Update: This is the model for the partial view:

public class SiteVisitDetailModel
{
    public String URL
    {
        get;
        set;
    }

    public List<DateTime> Dates
    {
        get;
        set;
    }
}

And this is the code from the parent view to add the partial view:

<% Html.Telerik().PanelBar().Name("PanelBar").HtmlAttributes(new { style = "padding-left: 0em;" }).Items(items =>
{
    foreach (var item in Model.Visits)
    {
        SiteVisitDetailModel model = new SiteVisitDetailModel();
        model.URL = item.Key;
        model.Dates = (from siteVisit in item
                             select siteVisit.Time).ToList();

        items.Add()
            .Text(item.Key.ToString() + " " + item.Count().ToString() + " visits")
            .LoadContentFrom("SiteViewDetail", "Report", model);        

    }
}).Render();

In the SiteVisitDetail action method, model.URL is properly set, and model.Dates is null.

+4  A: 

If I understood your problem correctly...

If your partial view can be strongly typed, its model could be the list, and you can do:

<%Html.RenderPartial("PartialView",myList);%>

Otherwise, the parent view can add the list to its ViewData, which would be accessible from the partial view.

Terje
Can I only pass a list directly to a strongly typed model? If so, how do I add the list to the parent's ViewData? I've tried doing both ViewData["test"] = "testing!" and ViewData.Add("test", "testing!") in the view itself, as well as in the controller method that returns the view. Neither of which has any value in the partial view.Ideally I"d like to just pass the list (or a model containing the list) to the controller method that returns the partial view. Is that not possible?
Mike Pateras
Terje
I'm sure that the data exists. If I access it in the main view, it appears just fine. For some reason, though, it ends up null in the controller, and I don't think it has any value in the partial view. I'll double check that last one, but does it make a difference whether I'm passing it to the partial view or the controller? And is there any way that I can verify the object before I pass it to the view? Putting a breakpoint there doesn't seem to work.
Mike Pateras
You have to explain "passing it to the controller", since that doesn't make sense unless your POSTing your data in a form (which your telerik component may or may not do, for all I know). But the basis premise is that the `Controller` builds the `Model`, which it passes to the `View`, so that the `View` can concentrate on presenting that data. So the view you present in your question should be able to do `Model.Visits[x].Dates` from the get-go, since it should be presented with a complete `Model`. When done this way, I know from experience that fetching data in Partial Views works as expected.
Terje
Sorry, I'm new to MVC and don't have all the terminology yet. Take a look here: http://tinyurl.com/ya6ug52. That shows the view, how I"m building the model, and how I'm passing it to Telerik's LoadContentFrom method, and the controller method. That is what I meant by "passing the model to the controller". When the control loads its content, the controller's SiteVisitDetail method is called, and when I set a break point on it, Model.URL is the correct value, and Model.Dates is null. As you can see, I'm not doing anything fancy with Model.Dates, just populating it with DateTime.Now.
Mike Pateras
The same is true in the partial view, not unexpectedly. Model.URL is properly set, Model.Dates is null.
Mike Pateras
The original question of "How can I pass a list from one view to another" has been answered. I'm going to start a new thread to address the problem that I'm having. Thank you for your help, Terje. Perhaps we can get this sorted out in the new thread (I'll link it here in a minute).
Mike Pateras
http://stackoverflow.com/questions/2044547/mvc-model-missing-data-in-partial-view
Mike Pateras
I've found the problem. See the post I linked in the previous comment, but it looks like the query string is being built incorrectly by the control.
Mike Pateras
Glad you found the problem :)
Terje
+1  A: 

Check this post out on how to pass models around.

Essentially you shoiuld probably pass a model to the view that contains your list. then you can extend it later on.

Multiple models sent to a single view instance

griegs
I think that's what I'm trying to do. The problem is, the list object is null when it gets to the controller. I saw somewhere that you couldn't pass "complex types" around, and had assumed that List (or IEnumerable) was such a type. This is not the case, I take it? I should be able to have a model with a list on it and I should be able to access that list in my controller method?
Mike Pateras
Yeah you should be able to pass around a complex type. I do. You may want to consider breaking the list into a PartilView like @Terje suggests. This this may aid you in debugging. The partial view will be strongly typed yeah but that shouldn't be an issue i hope.
griegs
I'll try making the model just the list for now and see where that gets me. Thanks for the suggestion.
Mike Pateras