简体   繁体   中英

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:

在此处输入图片说明

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?

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM