MVC having same controller and action name in area folder too - asp.net-mvc

I am having a action method in root folder New/NewForm. The partial view NewForm will be called from New controller... I have a New folder in Area folder too but the action method NewForm is not there in the controller NewController.. but while working from root folder i am invoking a ajax call for New/Newform but it is not getting called rather the application just hangs.. when i exclude the area folder "New" it is working fine. Any solutions would be great to me now as i need to solve this quickly..
Thanking you in advance.
Thanks,
Vin

If I'm not wrong
The ajax call which you are trying to make from your view will depend on the location of the view.
Suppose if your view is the Root Views then the url for the ajax call will be
url:'controller/actionmethod' I e url:'New/NewMethod',
If you want to make ajax call to a controller which is in an Area
For example Area name is Product
then the url will be url:'Product/New/NewMethod' Ie area/controller/actionmethod'
Thanks

Related

MVC4 - after adding new controller, corresponding view is not generated

I started learning Asp.NET MVC and got stuck pretty immediately. According to all materials I have, after I add controller in MVC project (Template - empty MVC controller), corresponding view should be created, too (folder under Views).
However, when I do this, nothing happens. Does anybody know what could be the problem?
And will this cause me a problems in a long run? I guess I could create those files manually, but still would prefer if they were generated...
My system:
Visual Studio Professional 2013, Update 5
Project: new MVC Web Application, template "Internet Application"
Thank you
Other possible way is to add complete path of the view in your controller method which is returning a view.
public ActionResult Index()
{
return View("~/Views/Home/Index.cshtml");
}
this method is useful when you have a hierarchy of folders for view files.
Normally views aren't created when a new controller is added. Views can be generated with the following methods:
Simply right click the method name within any controller and select "Add View"
Right click on the controllers folder and select "Scaffold..." which will created a new controller, views and/or DataContext for a specified model.

How to call Controller action method from umbracoo

I am new to Umbracoo . I was trying to call Controller action method from umbracoo . I find the solution for how to render view but did not find any way to call action method of controller ..
for this purpose first you would have to inherit your controller from Umbraco.Web.Mvc.SurfaceController or Umbraco.Web.Mvc.RenderMvcController
have a look at:
http://our.umbraco.org/documentation/reference/mvc/surface-controllers
http://our.umbraco.org/documentation/reference/mvc/custom-controllers
Make sure that your view/partialView is created from umbraco backend as template so that it would have it's record save in database through unique id.
it would be hidden in your solution explorer first show all hidden items and the include in your project and start working on it.

Problems returning PartialViews from controller

I am using MVC 4 and I have some areas in my project and some Views and Partial Views within each area:
Areas
AdminArea => one area and so on
Views
Customer
Customer.cshtml => my View
_CustomerDetails.cshtml => my partial View
In my controller, CustomerController I have the following code that fails:
return PartialView("_CustomerDetails", model) => fails to find my partial view.
However, if I call
return PartialView("~/Areas/AdminArea/Views/Customer/_CustomerDetails.cshtml", model)
the code executes successfully.
My Views (not partial views) all work ok. I even have some some partial views (also in the same area) that render ok without specifying the full path, but for some reason, for the most of them the above code fails in constructor saying that:
The partial view '_CustomerDetails' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Customer/_CustomerDetails.cshtml a.s.o. ... => and is searching in global not in Admin area.
Is there any way to fix this problem without having to specify the full path to my PartialViews in code? (like passing area="Admin" like I do in the .cshtml file).
I am not using any custom ViewEngine and I have called AreaRegistration.RegisterAllAreas() in Global.asax.cs.
Thanks,
Tamash
I am really sorry for even posting this question:
The call from the controller worked fine, but the problem was that I was calling from javascript to get the PartialView through an action and I didn't specify the full name for that action. That's why the partial view wasn't returned since the method responsible for returning it wasn't being called at all.
Sorry about that: the code is a bit complex and I got lost in details around Ajax calls.
seems weird, try adding a view from controller action method i.e right click action method in controller, add view, make a view a partial view and see where it adds it.. check if it is the same location as you are putting, if not there is some structure prob/bug.

Asp.Net MVC 3 - How to add/execute another view file

MVC Razor,
I have a View folder that contains
\Student\Create
\Student\Index
\Student\Delete...etc (default generated files) .
I wanted to create another view for example
\Student\Apply.cshtml
When I try to execute from e.g. Index to Apply file it shows file cannot be found.
Inside my Index file:
#Html.ActionLink("Apply", "Apply", new { id = #Model.ID })
How do I make it work? or it is not possible to have other view files except for the default generated files..
Thank you
You need to create an action that returns that view.
That's why it's called ActionLink.
Views are never shown on their own; they can only be returned by controller actions.
Have you created the Apply action in the Student Controller?
An actionlink points to an action which then renders a view.

The view 'Index' or its master was not found.

The view 'Index' or its master was not found. The following locations were searched:
~/Views/ControllerName/Index.aspx
~/Views/ControllerName/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
I got this error when using ASP.Net mvc area. The area controller action are invoked, but it seems to look for the view in the 'base' project views instead of in the area views folder.
What you need to do is set a token to your area name:
for instance:
context.MapRoute(
"SomeArea_default",
"SomeArea/{controller}/{action}/{id}",
new { controller = "SomeController", action = "Index", id = UrlParameter.Optional }
).DataTokens.Add("area", "YOURAREANAME");
This error was raised because your Controller method name is not same as the View's name.
If you right click on your controller method and select Go To View (Ctrl+M,Ctrl+G), it will either open a View (success) or complain that it couldn't find one (what you're seeing).
Corresponding Controllers and View folders name have the same names.
Corresponding Controller methods & Views pages should same have the same names.
If your method name is different than view name, return view("viewName") in the method.
Global.asax file contain the URL Route.
Default URL route like this.
"{controller}/{action}/{id}"
So,Try this.
1. Right click your controller method as below.
Example: let say we call Index() method.Right click on it.
2. Click Add View.. and give appropriate name.In this example name should be Index.
Then it will add correct View by creating with relevant folder structure.
Check the generated code at MyAreaAreaRegistration.cs and make sure that the controller parameter is set to your default controller, otherwise the controller will be called bot for some reason ASP.NET MVC won't search for the views at the area folder
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"SomeArea_default",
"SomeArea/{controller}/{action}/{id}",
new { controller = "SomeController", action = "Index", id = UrlParameter.Optional }
);
}
Where this error only occurs when deployed to a web server then the issue could be because the views are not being deployed correctly.
An example of how this can happen is if the build action for the views is set to None rather than Content.
A way to check that the views are deployed correctly is to navigate to the physical path for the site on the web server and confirm that the views are present.
The problem was that I used MvcRoute.MappUrl from MvcContrib to route the context.Routes.
It seems that MvcContrib routing mapper was uncomfortable with area routing.
You most likely did not create your own view engine.
The default view engine looks for the views in ~/Views/[Controller]/ and ~/Views/Shared/.
You need to create your own view engine to make sure the views are searched in area views folder.
Take a look this post by Phil Haack.
I had this problem today with a simple out of the box VS 2013 MVC 5 project deployed manually to my local instance of IIS on Windows 8. It turned out that the App Pool being used did not have the proper access to the application (folders, etc.). After resetting my App Pool identity, it worked fine.
right click in index() method from your controller
then click on goto view
if this action open index.cshtml?
Your problem is the IIS pool is not have permission to access the physical path of the view.
you can test it by giving permission. for example :- go to c:\inetpub\wwwroot\yourweb then right click on yourweb folder -> property ->security and add group name everyone and allow full control to your site . hope this fix your problem.
It´s still a problem on the Final release.. .when you create the Area from context menu/Add/Area, visual studio dont put the Controller inside de last argument of the MapRoute method. You need to take care of it, and in my case, I have to put it manually every time I create a new Area.
You can get this error even with all the correct MapRoutes in your area registration. Try adding this line to your controller action:
If Not ControllerContext.RouteData.DataTokens.ContainsKey("area") Then
ControllerContext.RouteData.DataTokens.Add("area", "MyAreaName")
End If
If You can get this error even with all the correct MapRoutes in your area registration and all other basic configurations are fine.
This is the situation:
I have used below mentioned code from Jquery file to post back data and then load a view from controller action method.
$.post("/Customers/ReturnRetailOnlySales", {petKey: '<%: Model.PetKey %>'});
Above jQuery code I didn't mentioned success callback function.
What was happened there is after finishing a post back scenario on action method, without routing to my expected view it came back to Jquery side and gave view not found error as above.
Then I gave a solution like below and its working without any problem.
$.post("/Customers/ReturnRetailOnlySales", {petKey: '<%: Model.PetKey %>'},
function (data) {
var url = Sys.Url.route('PetDetail', { action: "ReturnRetailOnlySalesItems", controller: "Customers",petKey: '<%: Model.PetKey %>'});
window.location = url;});
Note: I sent my request inside the success callback function to my expected views action method.Then view engine found a relevant area's view file and load correctly.
I have had this problem too; I noticed that I missed to include the view page inside the folder that's name is same with the controller.
Controller: adminController
View->Admin->view1.cshtml
(It was View->view1.cshtml)(there was no folder: Admin)
This error can also surface if your MSI installer failed to actually deploy the file.
In my case this happened because I converted the .aspx files to .cshtml files and visual studio thought these were brand new files and set the build action to none instead of content.
I got the same problem in here, and guess what.... looking at the csproj's xml' structure, I noticed the Content node (inside ItemGroup node) was as "none"... not sure why but that was the reason I was getting the same error, just edited that to "Content" as the others, and it's working.
Hope that helps
Add the following code in the Application_Start() method inside your project:
ViewEngines.Engines.Add(new RazorViewEngine());
I added viewlocationformat to RazorViewEngine and worked for me.
ViewLocationFormats = new[] {
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml",
"~/Areas/Admin/Views/{1}/{0}.cshtml",
"~/Areas/Admin/Views/Shared/{0}.cshtml"
};

Resources