i got stack on my project the problem is after adding new controller class and views for the actions unable to link the views with existing views
*
The resource cannot be found
.*
i have home and account controller classes which work well and i add new controller class users and i have to action methods index and adduser both have view but anytime when i try to retrieve view from the new controller class it dosnt found the pages.
eg <%= Html.ActionLink("users", "Index", "users")%> please help !!!
have you created views?
Guideline:
create controller controller_nameController
create action public actionresult action_name(){return View();}
create views Hit ctrl+m, ctrl+v or right click on the action whose view you would like to create.
4. check if you really have created a view named action_name in controller_name folder
Then you can create links: ("to display text", "action name", "controller name")
Hope this helps.
Related
I have set up a menu-controller to drive the top menu links based on which other controller is being used. Each other controller has a separate nested master page for each of its views.
so, i have a menu-controller with several methods that return viewresults, one per each controller, or "section" of the site. So currently each of these methods has its own view to render the menu. but each view to render the menu is the same code, the only thing that changes is the logic in the controller methods based on which links to render.
is there any way to have all of these controller actions target the same view? since the view is the same for all?
thanks
Yes, that is a common practice.
return View("Menu");
Create a strongly typed view that takes a container specifying your menu content. Pass this as a parameter on your return statement.
var thisMenu = CreateMenuForThisRequest();
return View ("Menu", thisMenu);
it depends on what version of ASP MVC you're using; with MVC 2, you can create an ascx control and use RenderAction
in your view you'll put something like
Html.RenderAction("Menu", "Navigation");
and have a navigation controller with a Menu actionresult
public class NavigationController : Controller
{
[ChildActionOnly]
public ActionResult Menu()
{
Menu model;//your menu
return PartialView("YourMenuAscxControlName", model);
}
}
I think if you're using MVC 1, the MVC Future project has the RenderAction but i'm not sure.
For my menu I use the RenderAction method
I'm also using the ActionOutputCacheAttribute from Steve Sanderson
http://blog.stevensanderson.com/2008/10/15/partial-output-caching-in-aspnet-mvc/
you will greatly increase your site loading time with this caching
I have an ASP.NET MVC solution which references to another class library, in which i have an MVC folder and inside of it "Controller", "Model" and "View" folders. I'd like to do something like
#Html.ActionLink("Text", "Index", "DllController")
which will link to the DllController/Index action in the dll and show a view from that lib. Debugging through this, I see that it goes into the Controller, but once it reaches
return View("MyView", model);
I receive the foolowing error:
The view 'MyView' or its master was not found or no view engine
supports the searched locations. The following locations were
searched: ~/Views/MyView/MyView.aspx
~/Views/MyView/MyView.ascx
~/Views/Shared/MyView.aspx
~/Views/Shared/MyView.ascx
~/Views/MyView/MyView.cshtml
~/Views/MyView/MyView.vbhtml
~/Views/Shared/MyView.cshtml
~/Views/Shared/MyView.vbhtml
~/Mvc/Views/MyView/MyView.cshtml
~/Mvc/Views/Shared/MyView.cshtml
I assume this is because the view is being searched for in the current assembly. Can i change this behavior to show my imported view correctly?
In a standard MVC application we have _Layout.cshtml and an Index.cshtml view. Now imagine if the user receives an email activation after a registration and now he/she clicks on that link.
The link points to /Account/ActivateAccount and after I process the activation, I wanted to redirect the user to the Login.cshtml partial view displaying a message.
I'm aware I can pass the message via the TempData["Message"] but what I don't know is how to redirect to a partial view and have that display inside the _Layout.cshtml instead that by itself.
At least this is what is happening when I call RedirectToAction("Login", "Home")
Note: all "home" based partial views are displaying within a < div id="divmain"> and my ideal solution would be to be able to decide what view should display inside that div, from the controller.
What's the right way to call a Partial View to be displayed from within another Controller ??
I'm not entirely sure I understand the question but I think what you're asking is if you can call a partial view from the _layout? This can be done with #Html.Action() helper. It will call out to a controller and method you specify and insert the result. The method you call would just be a PartialResult that returns the partial view with whatever data you need.
This is in contrast to #Html.Partial() which will render the partial view in place with whatever data you provide without routing back through a controller.
EDIT:
To summarize the comments to this answer, it seems I misunderstood the requirement. A user receives an email and clicks the link to activate their registration. The controller action that handles that request activates the user and then it needs to redirect to something. In this case, there was already a partial view which would serve the purpose but it couldn't be redirected to directly for some reasons. My suggestion was to just create a new View which contained a #Html.Partial() call to basically wrap the partial in a full view page.
The action method which handled the click could then just return that view directly or, if you consider the click of the link to be a "post" since it changes the application model by validating the user, you would create a new controller action and return RedirectToAction() directly. This would be like following the Post, Redirect, Get method and would prevent some issue if the user tried to refresh the "activated" page. It would also give more control over the URL naming.
I have a controller (AccountController.cs) whose final statement of its Index method is return View(model);.
In global.asax the only route I can see which relates to this is:
routes.MapRoute("AccountIndex", "Account/Index", new {controller = "Account", action = "Index"});
There is a Views folder, but it appears to only contain partial views and a master. When I right-click and select "Go To View" I get a "Cannot find view" message, although it seems to do this on all of the controller methods.
So where can I find this view? Where is it located? Any pointers?
It likely will map to the Index partial view, unless model is a string.
when you don't have view for controller action (for example: Index) you can click right mouse button on "return View();" in controller method to automatically generate view.
by default if you have controller with name "AccountController" and action "Index" the view should be in View\Account\Index.cshtml [if you use razor].
I've created an area that will handle our some generic things across all our development products, just as log ins, HTML helpers, etc. Inside the area, I have a partial view that I'm attempting to reference outside of the area. I've registered the area with
public class Routes : AreaRegistration
{
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Common_default",
"Common/{controller}/{action}/{id}",
new {
controller = "Account",
action = "Index",
id = UrlParameter.Optional
});
}
public override string AreaName
{
get { return "MvcCommons"; }
}
}
And now in the regular project, I'm trying to reference a view in the MvcCommons area...
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>TestGrid</h2>
<% Html.RenderPartial("jQGridTable", ViewData.Model); %>
But I keep getting that the view isn't found. Before, while creating the MVC Commons project, I was getting view errors, but the errors told me that it looked in both the area folders and the default view folders. This time, I'm only getting the default folders. Is there any way to accomplish this?
Thanks everyone!
I haven't actually had to do this, but at a guess I would assume you should use Html.RenderAction() instead, something like Html.RenderAction("action", "controller", new { area = "Area", model = ViewData.Model }).
model will have to be replaced with the name of the action's parameters, but that should work.
edit this will require a controller and view setup for each action though.
An important thing to remember is that when using RenderPartial you use it in the context of the current action.
As your action isn't in an area it will only look in the View/ folder for the controller the action belongs to then the shared folder.
Any views you wish to share between areas and controllers and have available at the route should be the root View/Shared folder. Really if the view is callable as a partial like that there is probably little reason for it to belong to an area.
You can call into area when you want to render actions rather than partials - which then changes the context of the current action to the action you call into thereby allowing you to then return views within that area.
The Default ViewEngine looks for the Views inside the same area (or root) folders where the user control is referenced. If you want to create a custom way to use or store views, I suggest you to create a custom ViewEngine. Please take a look at this example: Implement Theme Folders using a Custom ViewEngine