1

I've been exploring ASP.NET MVC 5 these days and got stuck into some basics. As an example, we create controller called 'HomeController' and in this regard, a view is created called 'Index' in the 'Home' folder. Now suppose we create another folder into the 'Home' folder named 'Data' and again it has a view called 'ViewData' . In the 'HomeController' , I've a method named 'ViewData' as follows (I want this method to be referred to the view 'ViewData'):

public ActionResult ViewData()
{  
   MainDbContext db = new MainDbContext();
   var con = (from c in db.Lists
              where c. Public == "No"
              select c). ToList();
   return View("Data/ViewData", con);
}

When I run the project, it works as desired I mean giving me the output and the URL looks like this: http://localhost:2361/Home/ViewNoData

But I want to show it like this: http://localhost:2361/Home/Data/ViewNoData as it is under 'Home' folder. One more thing - when I right click on the 'ViewData()' to match the view, it shows 'Unable to find the matching view'. How could I resolve it? Just to clarify if it is possible.

So below is the folder structure:

Views/Home/Data/ViewData.cshtml

And in the controller, the below:

Controllers/HomeController.cs

1 Answers1

0

You can use ASP.NET MVC Areas to achieve this:

  1. Right click the solution in Visual Studio -> Add -> Area
  2. Call it Home
  3. Go to the Controllers folder in the Home area -> Right click -> Add -> Controller
  4. Call it DataController
  5. Lastly create an action called ViewNoData in the DataController

Areas in MVC

Now you can access it like this - http://localhost:51118/Home/Data/ViewNoData

If you don't want to use Areas for some reason you could just tweak around with the routing in RouteConfig.cs

routes.MapRoute(
    name: "Default",
    url: "Home/{controller}/{action}/{id}",
    defaults: new { controller = "Data", action = "ViewNoData", id = UrlParameter.Optional }
); 
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
  • Thanks for the reply. Can it be done without using the Areas folder I mean usually what we do with the default folder structure in ASP.NET MVC? – DOTNET Rocker May 29 '16 at 07:27
  • In the file, right now, I've following routing structure as I've used the default folder structure already: routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); So will it create any conflict? – DOTNET Rocker May 29 '16 at 07:39
  • Home/{controller}/{action}/{id}?Is that what you have?Then it should work.If it doesn't then there's a conflict between that route and other routes.Then you must use areas – Denys Wessels May 29 '16 at 07:40
  • OK. If I create the areas folder, can I create action links from the files in the default folder for the new folder areas like this:

    @Html.ActionLink("Click Here To Proceed", "Home/Data/ViewNoData")

    – DOTNET Rocker May 29 '16 at 07:49
  • Yes you can but there's an extra parameter you must pass through with the area name.You can google for examples of this.And please remember to mark my answer as correct if it helped you – Denys Wessels May 29 '16 at 07:59
  • Sure. I'll. One thing and hopefully the last. Could you please show how to pass that parameter just with a simple example if it is possible and you have time? I've been googling around for few days and stuck. – DOTNET Rocker May 29 '16 at 08:06
  • http://stackoverflow.com/questions/2036305/how-to-specify-an-area-name-in-an-action-link – Denys Wessels May 29 '16 at 08:29
  • Thanks a lot @Denis Wessels. It worked. Thanks again for the co-operation. – DOTNET Rocker May 29 '16 at 08:47