Tamas Ionut
Tamas Ionut

Reputation: 4410

Problems returning PartialViews from controller

I am using MVC 4 and I have some areas in my project and some Views and Partial Views within each area:

Areas
    AdminArea   => one area and so on
        Views
           Customer
              Customer.cshtml         => my View
              _CustomerDetails.cshtml => my partial View

In my controller, CustomerController I have the following code that fails:

return PartialView("_CustomerDetails", model) => fails to find my partial view.

However, if I call

return PartialView("~/Areas/AdminArea/Views/Customer/_CustomerDetails.cshtml", model) 

the code executes successfully.

My Views (not partial views) all work ok. I even have some some partial views (also in the same area) that render ok without specifying the full path, but for some reason, for the most of them the above code fails in constructor saying that:

The partial view '_CustomerDetails' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Customer/_CustomerDetails.cshtml a.s.o. ... => and is searching in global not in Admin area.

Is there any way to fix this problem without having to specify the full path to my PartialViews in code? (like passing area="Admin" like I do in the .cshtml file). I am not using any custom ViewEngine and I have called AreaRegistration.RegisterAllAreas() in Global.asax.cs.

Thanks, Tamash

Upvotes: 0

Views: 1039

Answers (2)

Bhavin
Bhavin

Reputation: 427

seems weird, try adding a view from controller action method i.e right click action method in controller, add view, make a view a partial view and see where it adds it.. check if it is the same location as you are putting, if not there is some structure prob/bug.

Upvotes: 1

Tamas Ionut
Tamas Ionut

Reputation: 4410

I am really sorry for even posting this question: The call from the controller worked fine, but the problem was that I was calling from javascript to get the PartialView through an action and I didn't specify the full name for that action. That's why the partial view wasn't returned since the method responsible for returning it wasn't being called at all. Sorry about that: the code is a bit complex and I got lost in details around Ajax calls.

Upvotes: 1

Related Questions