I have a website with a front-end dependent on the _Layout file in MVC for navigation but it is not applicable in the back-end so I would like to use a custom Layout file for this.
I have seen this used often.
Layout = ViewData["~/Views/Shared/_AdminShared.cshtml"];
It would be placed in the view, this, however did not work for me.
Are there any better alternatives? Why does this solution not work?
_AdminShared.cshtml just contains the default _Layout content found on a new MVC project (for now)
You can directly use your custom layout by doing this without the ViewData:
#{
ViewBag.Title = "CustomLayout";
Layout = "~/Views/Shared/_AdminShared.cshtml";
}
Make sure that the layout is really existing under the Shared folder.
You can also directly declare it in your Controller specific to your desired View:
return View("Index", "~/Views/Shared/_AdminShared.cshtml", Model);
Related
I want to use one theme for Admin Panel and one theme is a separate which I want to show my visitors. I mean to say that there should be separate themes for visitor and Administrator of a website.
How to do in asp.net mvc?
Because in mvc we have only one file _Layout.cshtml and here we have to attach only one theme.
You can create as many layout files as needed. So in your case, it is a good idea to create an admin area which has it's own layout page in which you can use your admin theme css files
Areas are logical grouping of related functionality and files needed for that (controllers,views, styles,code etc).Areas provide a way to partition a large Web app into smaller functional groupings.
For your normal visitors, their pages/views uses the default layout and for the admin users, they get the views rendered from the admin area where it has it's own layout.
For the views from admin area (or even the _Viewstart.cshtml), you can explicitly specify the admin layout
#{
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
Define second layout in shared folder with another name like _AdminLayout etc. call the #RenderBody() function in it,
Now you can Render layouts via different ways,
1. Define layout in View:
#{
ViewBag.Title = "View_Title";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
2. Rendering layout page from ActionResult (using Controller. View extension method):
public ActionResult Action_Result_Name()
{
return View("Action_Result_Name","_AdminLayout");
}
3. Using _ViewStart.cshtml Page:
_ViewStart.cshtml page used to define the default layout page for the MVC application.
#{
layout = "~/Views/Shared/_AdminLayout.cshtml";
}
If you are using Identity framework then you can define layouts on the basis of conditions as well
#{
if (User.IsInRole("Admin"))
{
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
else
{
Layout = "~/Views/Shared/_Layout.cshtml";
}
}
I have an entire Web Application built using ASP.NET MVC and have a _ViewStart.cshtml page that specifies a Layout.
This works great for my entire site. However, I have a single prototype static HTML page that I just need to dump into a directory.
I copied the HTML into a CSHTML file and fronted it with a controller. The problem is that when I go to this page, it is using the Layout.
How can I configure it so that I can just serve this page up as static, standalone content without the Layout from _ViewStart?
By default, all views will use the layout from ~/Views/Shared as it is specified in the _Viewstart.cshtml file. Every time a view is executed, the code inside the _Viewstart.cshtml will be executed which sets the layout for the view.
If you do not want to execute/include the layout for a specific view, you can explicitly set layout as null on a view. Add the below code to the view.
#{
Layout = null;
}
Keep in mind that, even though it is static html in your cshtml file, user will not/should not directly access this view (like a normal html page/htm page). It has to be routed via an action method which returns this cshtml view.
Another option is to use the PartialView method instead of View method. When using the PartialView method to render a view, the framework do not run _ViewStart.cshtml, hence you get the same result.
public ActionResult About()
{
return PartialView();
}
PartialView is really handy when you want to render the markup for parts of your page (Ex : content of for a modal dialog etc)
In your static view page set layout = null.
Like:
#{Layout = null;}
I am Working On MVC4 architecture. I have a default layout i.e _Layout and models and view associated with it. It's Working fine.
Now Due to Some Requirement, after i Click on specific link i need it to redirect it a Whole different Layout and there i will associate models and view according to my Need.
But How to add a new layout and also keep the existing default layout in place.
For ex :- On Index page i gave a link Nikhil</a> -->. When i click this i have to go to a seperate layout and view.
Please Help in simplest and descriptive way possible because i am new to MVC4.
Thanks In Advance.
Yes, you can create multiple layout for different views if you want. In my case I have AdminLayout and UserLayout in my Shared folder. Just replace Layout variable.
Ex.
// For Admin Pages
#{
Layout = "~/Views/Shared/AdminLayout.cshtml";
}
//For User Pages
#{
Layout = "~/Views/Shared/UserLayout.cshtml";
}
By the way I use razor view engine here.
For Navigation you can create an ActionLink using HTML Heplers on your index page Like:
#Html.ActionLink("Link Text", "Controller","Action")
Now when you click on this link , the action specified of the controller will be accessed and in the action you can return the desired view, which has the different layout like :
public ActionResult Login()
{
return View("Desired View Name");
}
How can/should I create some "custom control" in ASP.NET MVC 3? I have red about partial views, ViewUsersControl, Html.RenderAction, but I still don't know, which way is the proper MVC way for razor views.
If I need to render some ajax component to view, I can imagine to do it by partial view, but what if I want to render section with custom logic?
1) PartialViews
2) Custom Html helpers
3) Child Actions
Update ASP.NET Core:
2) Tag Helpers are preferred way over Custom Html helpers
3) View Components are used instead of Child Actions
You may use
#{Html.RenderPartial("YourCustomView",YourModel);}
For instance, In your Shared folder create a new View and name it "_MyCustomControl"
Then in the code write :
#model YourNameSpace.Models.YourModel
#{
Layout = null;
}
#*Here write your control's markup*#
Then in your Views where you want to use this "control" you add:
#{Html.RenderPartial("_MyCustomControl",new YourModel { args });}
If you get into trouble with RenderPartial check this out
In addition to all the other answers for this post, I can offer you the use of EditorFor and DisplayFor templates. These are useful when you want to easily render or edit a custom type. It'll handle validation nicely (which can get weird when using partials) and you can nest them recursively (again another feature that isn't obviously handy until you need it).
You can also use Html.RenderAction() or Html.Action() to call another controller action within a view and display the results in-line in the page. This is probably the closest to what you need as it allows you to render a partial, include code in the controller and it also allows for the passing of parameters.
Links to:
DisplayFor and EditorFor Templates
Action and RenderAction
As you have mentioned that you can use Partial Views.
Yes you can use Partial Views, which is the most effective and efficient way.
For Ajax rendering you can always use
#using (Ajax.BeginForm("Details", new { id = Model.Post.PostId }, new AjaxOptions
{
Few of the links you would like to see
Rendering partial view in ASP.Net MVC3 Razor using Ajax
Render Partial Views using JQuery in MVC3
When I use #RenderBody on a view (not principal), I receive this message Error: The file "~/Views/Shared/_Sistema.cshtml" cannot be requested directly because it calls the "RenderBody" method.
I do not understand it as I am a novice in MVC.
What do I can do?
Thanks!
If you are using the Renderbody in _Sistema.cshtml file, then make it as a Layout page.
And add another partial page named like MyPartial.cshtml with Layout name as _Sistema.cshtml.
Renderbody is supposed to be in the master page only. i.e., Layout page.
So your _Sistema.cshtml page should only contains the following:
#RenderBody()
#RenderSection("scripts", required: false) #*----Optional---*#
Then your your new partial page MyPartial.cshtml should contain the following:
#{
Layout = "~/_Sistema.cshtml";
}
Then use your partial page in your view as follows:
#Html.Partial("MyPartial")
Hope it helps.
RenderBody is for masters only. This method renders markup of content pages that does not belong to any particular section. If your view calls RenderBody, two cases are possible:
Either this is a mistake and this view should not call it.
Or this view is a master, and you should instead use some other views inheriting layout from this master.
you just need to interact with _ViewStart.cshtml
and use if condition to specify the share mater page for each group of users.
for example user is admin then user _Layout.cshtm other wise use _Layout2.cshtml
use following code :
#{
if(User.Identity.Name.ToLower()=="admin") {
Layout = "~/Views/Shared/_Layout2.cshtml";
}
else {
Layout = "~/Views/Shared/_Layout.cshtml";
}
}
In my experience, I was struggling with this same issue for days. After further investigation, I found that when starting a new Umbraco site, I had the option to select templates. That resolved the RenderBody issue. When creating the layout page without the templates it doesn't see the Master page as the layout, hence not being able to call the RenderBody method. Using the templates automatically sets the Master page as the Layout allowing you to call the RenderBody. I hope this helps.