Domanda

I'm using Umbraco 6, and am using the new MVC architecture. I have a document type view template, which has a view model, which is instantiated and has it's properties populated from the controller. One of these properties is a collection, and in the view template I iterate through the collection and render out a Partial View with a separate view model, using 'Html.Partial("partialName", modelObject)'

The weird problem I'm having is that firstly, in Visual Studio I get a ReSharper warning telling me that it cannot resolve a partial view with that name (I have checked 50 times and I've spelt it correctly). Additionally when I then navigate to the page, I get the trusty ol' "Object reference not set to an instance of an object" YSOD.

I have debugged the code, and the controller action is hit fine, the logic to instantiate the view model for the document type template works fine, and populates the properties correctly, I've also made sure the properties are still set inside the view and the loop for rendering out the collection items correctly instantiates a view model object per collection item and sets the properties correctly. It breaks when it hits the Html.Partial.

Document Type View Code below:

@foreach (KeyValuePair<decimal, IPublishedContent> result in Model.Results)
{
    PropertySearchResultViewModel model = ObjectMapper.SearchResultToViewModel(result);

    Html.RenderPartial("PropertySearchResultDesktop", model);
}

Partial View Code below:

@using Production.Umbraco.Extensions.Models.ViewModels;
@inherits UmbracoViewPage<PropertySearchResultViewModel>

<article id="property-result-@Model.Node.Id.ToLower()">

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

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

</article>

Here is a screenshot of my VS Solution tree:

Visual Studio solution screenshot

The 'NewHomes.cshtml' document type view template is returned from the NewHomesController and the view is calling the 'PropertySearchResultDesktop.cshtml' partial view, which was created from the Umbraco back office, and was placed their automatically.

The Umbraco website says that you can and should place your partial views here

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

But no matter what I try to do, it just wont render the partial. I've seen one other question about this on SO but the answer was just to place it in MacroPartials instead, which I dont want to do as part of the benefit of using Partial Views in Umbraco 6 is that they inherit from UmbracoViewPage with a strongly typed model declaration, which MacroPartials don't.

Has anyone encountered this before?

È stato utile?

Soluzione

Fixed. The issue was with the model I was passing to the document type view from the controller.

In the Umbraco documentation it says, you can create a controller to hijack the Umbraco route and serve up your own view with a custom model, like so:

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

and in my view I had:

@inherits UmbracoViewPage<SearchResultsViewModel>

However, it seems as though, in order to do that, you must make sure your custom view model in inherits from RenderModel with a constructor that takes RenderModel as a parameter and then sets some properties on the base object, like so:

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

    }
}

Previously, my view model had not inherited from anything and had a parameterless constructor.

This article led me to the right answer.

http://www.ben-morris.com/using-umbraco-6-to-create-an-asp-net-mvc-4-web-applicatio

Also, as a side note, I still get the ReSharper warning of "Cannot resolve partial view PropertySearchResultDesktop" but I think that's a ReSharper bug rather than an error.

Even with a full path and file extension in the call, it still complains.

I do find it odd though that while debugging, even with my old controller code, no exception was thrown at the model binding stage or inside the controller, or in the view until it go to the Html.Partial call.

Anyway, I hope this helps anyone having the same issue.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top