0

I was working in a sample MVC project, i have used the below url action method and its not going to the URL as expected.

I have two views in total- named create and index of person entity. URLs like; app/Person/Create & app/Person/Index.

Here person is controller which is created as class name : PersonController

Now am trying to load Index view back to user, when user clicks on cancel button.

    $("#btnCancel").click(function () {
        window.location = '@Url.Action("Index", "Person")';
    });

and in person controller i have action defined in name Index.

But the url action returns me the url as :- app/person

Also calling this url action hits the breakpoint at the action Index of person coltroller class. And i am expecting the URLs like app/Person/Create & app/Person/Index.

Route config.cs - default one which come is mvc projects:

public static void RegisterRoutes(RouteCollection routes)

    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

Can someone help in this issue.?

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
pvaju896
  • 1,397
  • 6
  • 25
  • 46

1 Answers1

1

After compiling the code,

 $("#btnCancel").click(function () {
    window.location = '@Url.Action("Index", "Person")';
});

becomes like this:

enter image description here

But after compiling the code,

 $("#btnCancel").click(function () {
    window.location = '@Url.Action("create", "Person")';
});

becomes like this:

enter image description here

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
  • I could understand the explanation but, wats the reason of the person/index page's URL is skipping index from URL. Is it because of route config. – pvaju896 Jun 04 '16 at 06:21
  • Yes it is because of the default route.config config i guess. i changed the name of my view. and it is working fine. :-) – pvaju896 Jun 04 '16 at 06:26
  • ali, thnQ very much, and could you pls confirm if this is because of this route configuration.? – pvaju896 Jun 04 '16 at 06:27