Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Damian Green 452 posts 1433 karma points
    Feb 27, 2014 @ 17:43
    Damian Green
    0

    Html.Partial Model and ViewDataDictionary

    When you create a Partial you can pass the Model (IPublishedContent) into the partial or you can pass a ViewDataDictionary in there.

    However i thought you could pass both as the API has the following method:

    Html.Partial(string PartialViewname, object Model, ViewDataDictionary<T> viewData)

    But if i pass in the Model.Content AND a view data and my model inherits from UmbracoViewPage<IPublishedContent> the view is expecting a ViewDataDictionary of type IPublished content.

    I looked at doing soemthing like this but it doesnt like it. The ViewData datas its of type string but thinks its an object and @Model.Name is invalid as it thinks Model is a string.

    @model IPublishedContent
    @inherits UmbracoViewPage<string>
    @{
        string colour = ViewData["colour"];
    }
    Name = @Model.Name

    Slightly confused now as im sure ive passed in both before.

    Can anyone clear this up?

     

  • Damian Green 452 posts 1433 karma points
    Feb 28, 2014 @ 11:22
    Damian Green
    0

    Anyone know how to use this API call?

    Html.Partial(stringPartialViewname,objectModel,ViewDataDictionary<T> viewData)
  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Mar 01, 2014 @ 21:29
    Andy Butland
    102

    Like this I believe:

    Html.Partial("MyPartial", myModel, new ViewDataDictionary{ {"colour", "green" } });

    Not 100% sure but your first issue might be just that you need to cast to a string rather than assign it.  So rather than:

    string colour =ViewData["colour"];

    Try:

    var colour =ViewData["colour"].ToString();

    Or:

    var colour = (string)ViewData["colour"];

    Andy

  • Damian Green 452 posts 1433 karma points
    Mar 03, 2014 @ 12:47
    Damian Green
    0

    Doh! Damn intellisense! :)

    I was going on what the intellisense was giving me.

    Done just this and it works fine. :)

    @inherits UmbracoViewPage<IPublishedContent>
    @{    
        var colour = (string)ViewData["colour"];
    }

    Thanks Andy.

Please Sign in or register to post replies

Write your reply to:

Draft