Ra3IDeN Ra3IDeN - 29 days ago 13
C# Question

Application can't find view when using areas

I am experimenting with Areas and I have an ajax call that calls an action
but when trying to return a Partialview it says it can not find it:

enter image description here

UserAreaRegistration.cs :

public class UsersAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Users";
}
}

public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "User",
url: "User",
defaults: new { controller = "User", action = "CreateUser" }
);
context.MapRoute(
"Users_default",
"Users/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}


In my user controller I tried:

[RouteArea("User")]
public class UserController : Controller
{}


the action:

public ActionResult GetUsrForm()
{
var model = new UserMod();

return PartialView("testUUU");
}


but i still get:


The partial view 'testUUU' was not found or no view engine supports
the searched locations. The following locations were searched:
~/Views/User/testUUU.aspx ~/Views/User/testUUU.ascx
~/Views/Shared/testUUU.aspx ~/Views/Shared/testUUU.ascx
~/Views/User/testUUU.cshtml ~/Views/User/testUUU.vbhtml
~/Views/Shared/testUUU.cshtml ~/Views/Shared/testUUU.vbhtml


What am I missing?

Answer

I faced the same Issue before when I was dealing with Areas and I solved it by adding Area name in Ajax URL

$.ajax({
    url: 'Users/user/GetUsrForm',
    success: function (data) {
        //
    }
});

The Action

public ActionResult GetUsrForm()
{
    var model = new UserMod();

    return PartialView("~/Areas/Users/Views/User/testUUU.cshtml");
}