_Viewstart with controller action - c#

So my current (and I believe this is the default way) to use _ViewStart.cshtml is to do this:
Layout = "~/Views/_SomeSideMenu.cshtml";
Now my sidemenu is a bit complicated so it requires a controller. But I cannot figure out how to call a controller action for the ViewStart and let it return the PartialPage for it.
My current workaround involves using #Html.Action() in the _SomeSideMenu.cshtml file and fill TempData with the things I need.
Normally I would want to put that data in the ViewBag or Model, but these are not available since the Html.Action does not return the _SomeSideMenu.cshtml.
How would one go about calling the controller action to return the PartialPage that is found in the _ViewStart page?

Actually, you cannot do this in the ViewStart file. ViewStart is a file that can store common functionality related to all the views. These pages can also be nested, so if you have multiple controllers, each controller’s views can have their own ViewStart page.
You should add your sidemenu into a Layout page. You could add some conditions to have this output into layout page. For sample, jere, we have an action filter which add a key into ViewBag and we use this key to have a state for a sidemenu (state like open or close and position in left or right).
You also could change your default layout page in your view just doing what the ViewStart page does, chaging the Layout property.
Layout = "~/Views/Shared/CustomLayoutPage.cshtml";

Related

making Login Identity View in an independent Layout in .NET Core

I am trying to make a login view independent from the Default _Layout (like any proper app) .
i have achieved to make the login as default view using
options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "");
My problem is i want to make the /accout/login as the default view with out the _layout page .
so i go to areas/identity/pages/_ViewStart.cshtml and changed the Layout from
"/Views/Shared/_Layout.cshtml"; to "/Views/Account/Login";
the problem is that the controller render to account/login . but i can only see the button register and login as shown in the attached image .
So can any one guide me how to make a login as independent view ?
You can bypass any reference to a Layout page by setting the reference to null in your .cshtml page.
Your code would look like this #{ Layout = null; }.
By setting Layout to null your .cshtml page is no longer rendered into a parent layout, i.e., partial views are no longer partials.
Keep in mind that once you remove the Layout reference, any css styles etc. will no longer apply.
In your sample code, it looks like you are still pointing to _loginPartial.cshtml which just shows either register + login OR applicationUser link + logout option. What happens when you click on the login button??

How to use one razor view for two action methods but with slightly different contents in some portions only? [duplicate]

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

How to specify global MVC layout without specifying layout path in razor view

I'm trying to get away from having to manually specify the layout path in every Razor view that I have / create.
So in a razor view, you would normally specify the view / layout properties such as:
#{
ViewBag.Title = "About Us";
Layout = "~/Views/Shared/_ContentLayout.cshtml";
}
I have a base controller that all my controllers are inheriting, in which I would love to be able to specify the layout at this level, or alternatively in app_start etc.
For any exceptions I would just override this in the view itself.
After an extensive search, I haven't found any evidence of anyone being able to do this yet.
My current, next-best workaround is to specify this in the ViewBag, to keep it dynamic, but I still need to put a declaration in the view:
#{
Layout = ViewBag.Layout;
}
Is it possible? Solutions?
Reference: http://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts
Since MVC3 there is a convention where...
You can add a file called _ViewStart.cshtml (or _ViewStart.vbhtml for VB)
underneath the \Views folder of your project:
The _ViewStart file can be used to define common view code that you
want to execute at the start of each View’s rendering. For example, we
could write code within our _ViewStart.cshtml file to programmatically
set the Layout property for each View to be the _ContentLayout.cshtml file
by default:
#{Layout = "~/Views/Shared/_ContentLayout.cshtml";}
Because this code executes at the start of each View, we no longer
need to explicitly set the Layout in any of our individual view files
(except if we wanted to override the default value above).
As mentioned by Nkosi, if you want to adapt the layout to a per controller basis without specifying the layout path in the view, you could do a condition block in the _ViewStart file.
But what I've found also works and is a tiny bit easier...
Place a _ViewStart file in each view folder (which relates to a controller), which you want to have a different layout.
The more specific _ViewStart in the View Area folder overrides the global _ViewStart file.

Controller that calls a partial view that should display inside the Layout view

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.

How to make a View render Fully or Partially?

I have a HomeController with an action About. I want to achieve this behavior:
Case #1 User open the page /home/about and the view render fully (return View();)
Case #2 User is on the homepage and clicks a link about, I want to change the url using history.pushState and render it partially (return PartialView();)
How can I achieve this behavior?
You can use Request.IsAjaxRequest to detect whether the controller action was invoked using an Ajax request.
As an alternative I'd recommend splitting your views so into smaller pieces, such that your About.aspx view uses the partial view, e.g. by doing Html.RenderPartial( "AboutBox" ). This gives you the power of reuse without having to clutter your actions with if-sentences.
yet another.
Restrict ASP.NET MVC Action's Using The ActionMethodSelectorAttribute
select action method control at ActionMethodSelectorAttribute.

Categories