asp.net mvc - Define Custom View From Controller -


i've been exploring asp.net mvc 5 these days , got stuck basics. example, create controller called 'homecontroller' , in regard, view created called 'index' in 'home' folder. suppose create folder 'home' folder named 'data' , again has view called 'viewdata' . in 'homecontroller' , i've method named 'viewdata' follows (i want method referred view 'viewdata'):

public actionresult viewdata() {      maindbcontext db = new maindbcontext();    var con = (from c in db.lists               c. public == "no"               select c). tolist();    return view("data/viewdata", con); } 

when run project, works desired mean giving me output , url looks this: http://localhost:2361/home/viewnodata

but want show this: http://localhost:2361/home/data/viewnodata under 'home' folder. 1 more thing - when right click on 'viewdata()' match view, shows 'unable find matching view'. how resolve it? clarify if possible.

so below folder structure:

views/home/data/viewdata.cshtml

and in controller, below:

controllers/homecontroller.cs

you can use asp.net mvc areas achieve this:

  1. right click solution in visual studio -> add -> area
  2. call home
  3. go controllers folder in home area -> right click -> add -> controller
  4. call datacontroller
  5. lastly create action called viewnodata in datacontroller

areas in mvc

now can access - http://localhost:51118/home/data/viewnodata

if don't want use areas reason tweak around routing in routeconfig.cs

routes.maproute(     name: "default",     url: "home/{controller}/{action}/{id}",     defaults: new { controller = "data", action = "viewnodata", id = urlparameter.optional } );  

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -