Copied to clipboard

Flag this post as spam?

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


  • Justin Moser 20 posts 92 karma points
    Jul 13, 2013 @ 19:30
    Justin Moser
    0

    "Cannot resolve partial view X"

    I'm trying to render a partial view from a document type script file, and not only do i get a ReSharper warning teling me "Cannot resolve partial view" I then get an "object reference to set to in instance of an object" YSOD error.

    Firstly I thought I had mispelled the partial view name (which by the way is the the Views/Partials/ folder in my solution) but regardless of what I name the partial I get the same error.

    If I move the partial view to "Views/Shared" the "cannot resolve partial view" warning dissapears but I still get the sme YSOD error.

    Anyone encountered this before? 

    Thanks! :)

  • Charles Afford 1163 posts 1709 karma points
    Jul 14, 2013 @ 20:33
    Charles Afford
    0

    What are you using MVC?  Are you using a surface controller?  Is the partial view not rendering?  Does the view render?  What is the partial view name and what is the call you are making to your controller/action if you have one. 

    Charlie. :)

  • Justin Moser 20 posts 92 karma points
    Jul 14, 2013 @ 20:59
    Justin Moser
    0

    Hi Charlie,

    Yes I have the view engine set to MVC. No Im not using a surface controller, or any controllers for that matter, I am just trying to render the partial from a document type template, passing in a view model into each call to the partial view.

    As for the view rendering, it does not, on the page itself that calls the partial view I get a YSOD error telling me 'object reference not set to an instance of an object'.

    I have a page that takes a querystring parameter and goes off and gets some data from a separate db, then comes back and compares the data against nodes in Umbraco, then for each node that is a valid result, it creates a view model with the IPublishedContent as a property and a few other properties from the database results, iterates through those, rendering an instance of the PartialView for each one. (The partial view's model is the view model)

    I'm not calling the partial view from a controller or action, just from a page template (i think this may be the issue, that I have misunderstood how MVC works with umbraco) and the partial view is called 'PropertySearchResultDesktop'

    I am calling @Html.RenderPartial("PropertySearchResultDesktop", dto) within a foreach loop (the dto object is set above)

    Thanks for your response :)

  • Charles Afford 1163 posts 1709 karma points
    Jul 15, 2013 @ 15:38
    Charles Afford
    0

    Hi, i have never done this but as you say it sounds like you are dropping out of context somewhere along the line.  But you partial should be in View/MacroPartials.  If you debug and examine the dto object?  Has it been instanciated?

    Where is the db logic being handeled?

    Maybe you need to set the dto object on the GET.  By this i mean look at route hijacking http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers

    so before the page is rendered it goes in to this GET action does some stuff and then trys to render the view/partial.

    Hope this helps.  Let me know how it goes. :).  Charlie

  • Justin Moser 20 posts 92 karma points
    Jul 15, 2013 @ 21:03
    Justin Moser
    0

    Thanks for the help, I'm now using a surface controller which returns the view and page template with a custom view model. All working hunky dory in that respect. One of the properties on the viewmodel is a set of results (based on a search from querystring parameters) the results are populated fine, and then i loop through the results from the view model in the template, rendering out an instance of the partial for each result.

    View Code below:

    foreach (KeyValuePairresult in Model.Results)

    {

        PropertySearchResultDto dto = ObjectMapper.SearchResultToDto(result);

        Html.RenderPartial("PropertySearchResultDesktop", dto);

    }

     

    Partial View code:

    @using Production.Umbraco.Extensions.Models.DTO;

    @inherits UmbracoViewPage<PropertySearchResultDto>

    <article id="[email protected]()">

    <p>@Model.Node.Name</article>

    <p>Distance: @Model.Distance Miles</p>

    </article>

     

    The Partial View is in Views/Partials/PropertySearchResultDesktop.cshtml

    I've debugged the view being rendered, and the route successfully gets handled by my controller, and the 'Results' property on my view model gets set correctly, the dto object is also instantiated correctly, the error comes when it gets to the Html.RenderPartial...

    In Visual Studio, resharper complains that the Partial View "PropertySearchResultDesktop" doesnt exist.

    I'm really stumped with this so im hoping someone has had this issue before!

  • Charles Afford 1163 posts 1709 karma points
    Jul 15, 2013 @ 22:52
    Charles Afford
    0

    can you take a screen shot of your vs solution so we can see the file names and how what you have setup in solution?

    I think i know the problem.  Are you opening the umbraco wwwroot in VS?  using open website?

    In the umbraco backend can you show me the structure of the templates?  do you have one in there for PropertySearchResultDesktop.

    What i would try is:

    In Visual Studio:

    Backup PropertySearchResultDesktop

    Delete PropertySearchResultDesktop

    In Umbraco:

    Delete the PropertySearchResultDesktop template

    Recreate the PropertySearchResultDesktop

    In Visual Studio:

    You should now have a template in your views folder.

    Hope this will help :) let me know :D.  Charlie.

     

     



  • Justin Moser 20 posts 92 karma points
    Jul 15, 2013 @ 23:40
    Justin Moser
    0

    Hi Charlie,

    Screenshot attached. The problem is, it's not a page template, its a Partial, with a viewmodel. It's supposed to be reusable block of code in the form of a partial view, which has it's own view model.

     

  • Charles Afford 1163 posts 1709 karma points
    Jul 16, 2013 @ 00:19
    Charles Afford
    0

    That needs to go into the macro partials if you want to use it with Umbraco i think.  I have always put them in there.  Have always had trouble when adding them to the Partials folder.

  • Charles Afford 1163 posts 1709 karma points
    Jul 16, 2013 @ 00:20
    Charles Afford
    0

    did you try the stuff i said with vs and umbraco?

  • Justin Moser 20 posts 92 karma points
    Jul 17, 2013 @ 11:08
    Justin Moser
    0

    Hi Charlie,

    I created the partial from the Umbraco office, which put it in the Partials folder itself, and inherited the view from UmbracoViewPage.

    According to this page: 

    http://our.umbraco.org/Documentation/Reference/Mvc/partial-views

    Partial views should be placed in the partials folder, which then enables me to use the strongly typed features of inheriting the view from UmbracoViewPage

    I tried placing it in the MacroPartials folder, and it works in principle, but I need to be able to pass in a strongly typed view model, and MacroPartials use the 'ViewData' object.

     

    Thanks,

    Justin

  • Jason Prothero 422 posts 1243 karma points MVP c-trib
    Jul 18, 2013 @ 20:43
    Jason Prothero
    0

    Can you get a partial to work that doesn't pass in a model?

     

    Thanks,

    Jason

  • Justin Moser 20 posts 92 karma points
    Jul 19, 2013 @ 10:49
    Justin Moser
    0

    Hi Jason,

    Your question led me to the answer (for the YSOD error anyway, the ReSharper warning is still there, but I think that's a ReSharper bug).

    Basically, in my parent document type view (NewHomes.cshtml) I had it inheriting UmbracoViewPage

    However, in my controller, for the Index Action, I had the Umbraco 'RenderModel' model type as a parameter to the action, and was then creating a new instance of my custom view model inside the action and returning it to the view.

    Weirdly, the Umbraco documentation does say you can do that, but it also says if you want your custom view model to have access to all the RenderModel functionality (.Content etc) then you should inherit your view model from this, like so:

     
    public class SearchResultsViewModel : RenderModel
    {
            public SearchResultsViewModel(RenderModel model) : base(model.Content, model.CurrentCulture)
            {
     
            }
    }

     
     
    public ActionResult Index(RenderModel model)
    {
    SearchResultsViewModel viewModel = new SearchResultsViewModel(model);
    }

     

    It's weird because when I was debugging it never threw the exception at the model binding stage, just at the point I tried to render the partial.

Please Sign in or register to post replies

Write your reply to:

Draft