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:
- right click solution in visual studio -> add -> area
- call home
- go controllers folder in home area -> right click -> add -> controller
- call datacontroller
- lastly create action called viewnodata in datacontroller
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
Post a Comment