MVC 4: Create controller for Shared view _Layout.cshtml - asp.net-mvc

How do you achieve this?
This View is automatically generated.
When I manual add a View for example: Contact view with Index.cshtml file in it.
I can modify this view by writing a controller Contact.
public class ContactController : Controller
{
public ActionResult Index()
{
#ViewBag.Test = "this text will be used in my Contact View";
return View();
}
}
So in my contact view i can do like this
<p> #Viewbag.Test </p>
And the text will be displayed.
But how do you achieve this for my _Layout.cshtml file in my Shared View?
I tried the same by adding a SharedController but not working this way

You can't have a controller for _Layout.cshtml. This file is used for any view's layout. For example, look at the _ViewStart.cshtml file in your Views folder:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
This tells basically all controllers to use that layout as a wrapper around the view returned by your controller actions.
Your _Layout.cshtml file already has a hint about one way to populate it with values:
<head>
...
<title>#ViewBag.Title</title>
...
</head>
If you do the following in a view, it will be rendered in the head/title section of the _Layout.cshtml file:
#{
#ViewBag.Title = "Home";
}

You don't need a controller for _Layout. Your contact view is added to _Layout to create one complete view. So you can use any of your ViewBag properties from your contact controller inside of _Layout also. _Layout has access to the same variables as your contact view.
Specifically, in your example:
public class ContactController : Controller
{
public ActionResult Index()
{
#ViewBag.Test = "this text will be used in my Contact View";
return View();
}
}
ViewBag.Test will also be accessible in _Layout the same way it is in your contact view.

Related

Layout page not loading in asp.net mvc

I've created a strongly typed view selected a layout page
#{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_xyzLayout.cshtml";
}
but this layout page never gets loaded when I run the page
Layout Snapshot
#RenderSection("scripts", required: false) u can call this code inside the layout page
Then u call every page #Rendersection scripts{
Your scripts}
#RenderSection("styles", required: false) also
http://www.dotnettricks.com/learn/mvc/layouts-renderbody-rendersection-and-renderpage-in-aspnet-mvc
Refer this page
Ensure the controller routing to the page uses a View method call. For example
public ActionResult Details(int custid )
{
Customer customer = new Customer(id =2, Name = "Dipo")
return View(customer);
}

MVC: How to get controller to render partial view initiated from the view

In my MVC5 project I want to create a menu in a partial view. This menu is dynamic in the sense that it is built from content in my database. Thus I have a controller taking care of creating my menu and returning the menu model to my partial view:
public PartialViewResult GetMenu()
{
MenuStructuredModel menuStructuredModel = menuBusiness.GetStructuredMenu();
return PartialView("~/Views/Shared/MenuPartial", menuStructuredModel);
}
In my partial view called MenuPartial I want to use razor to iterate over my menu items, like:
#model MyApp.Models.Menu.MenuStructuredModel
<div class="list-group panel">
#foreach (var category in Model.ViewTypes[0].Categories)
{
#category.ShownName
}
</div>
Now the problem is the view in which I insert the partial view. If in the view I simply do:
#Html.Partial("MenuPartial")
It won't call the controller to populate the model with data first. What I want is to let the controller return the partial. But I don't know how to do this from the view. In pseudo code I would like to do something like:
#Html.RenderPartialFromController("/MyController/GetMenu")
Thanks to Stephen Muecke and Erick Cortorreal I got it to work.
This is what the controller should look like:
[ChildActionOnly]
public PartialViewResult GetMenu()
{
MenuStructuredModel menuStructuredModel = menuBusiness.GetStructuredMenu();
return PartialView("~/Views/Shared/MenuPartial", menuStructuredModel);
}
And it may called like:
#Html.Action("GetMenu", "Home")
(Hence GetMenu() is declared in the HomeController in my example).
The controller is now called (and the model is populated) prior to the partial view is rendered.
You should use: #Html.RenderAction or #Html.Action.

no selected value selectlist partial view asp.net mvc

So I have a Edit view with a partial view. In this partial view I have a selectlist (or dropdownlist) which values come from a ViewBag. In the control I include the selected value but it just does'nt work.
public ActionResult Edit(int id = 0)
{
Customer c = db.Customer.Find(id);
ViewBag.CustomerGlobalQuality = new SelectList(db.GlobalQuality, "Id", "Quality", c.Skill.GlobalQuality);
return View(c);
}
and in the PARTIAL VIEW I have:
#Html.DropDownList("CustomerGlobalQuality")
#Html.ValidationMessageFor(model => model.Skill.GlobalQuality)
what did I miss? It usually works with normal views, so why not with a partial?
if your logic doesn't need partial view, do not include it in this case. Use those lines of code inside your edit chtml view. In your case debug it and see what are you realy sending to drop down list. I see that you included dropdown list dana in edit view. If you use partial view you must pass object to that view in controler.
public ActionResult PartialDDLData()
{
ViewBag.CustomerGlobalQuality = new SelectList(db.GlobalQuality, "Id", "Quality", c.Skill.GlobalQuality);
return Partial("_nameOfView",ViewBag.CustomerGlobalQuality);
}
and make sure your partial view is accessible in shared or controller view folder.

unable to set layout page for a view

I have created a helloWorldController, for which I have created a index method
Here is the view code which is generated for me
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
and my _layout page is default ASP.NET MVC 4 layout.
But still I am getting blank page with the string returned from the Index method.
How can I enable _Layout page for a view?
If that screenshot is the actual code for your controller, you need to return ActionResult for each action method rather than string, e.g.
public ActionResult Index()
{
return View();
}
The View() return value will work provided that there is a view called Index.cshtml in either your Views\HelloWorld or Views\Shared folder. The convention is that you have a specific folder with the same name as your controller for all views specifically relating to that controller - in your case HelloWorld

Putting an ID on <body> in ASP.NET MVC

I would like my views to be able to specify a class for the <body> tag, which lies in my master page.
My first take was to do something like this:
<asp:Content ContentPlaceHolderID="BodyClassContent" runat="server">
view-item</asp:Content>
However, that would require this in the master page, which doesn't work:
<body class="<asp:ContentPlaceHolder ID="BodyClassContent" runat="server" />">
Any solutions to this?
In the layout you can do this on the body tag:
<body #RenderSection("BodyAttributes", false)>
and then in your view you can do this:
#section BodyAttributes {
id="login" class="login"
}
Edit: I also had to do this working with VB.NET and WebForms today and found a handy link for achieving the equivalent
I would suggest a different approach.
You create an hierachy of view models, starting with the MasterModel. When you instantiate a view object, you pass a body class to it.
public class MasterModel
{
string BodyCss { get; set; }
public MasterModel (string bodyCss)
{
BodyCss = bodyCss;
}
}
public class MyView1Model : MasterModel
: base ("body-view1")
{
}
Then in your master view which should be strongly typed to MasterView:
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<MasterModel>" %>
you just write:
<body class="<%= Model.BodyCss %>"></body>
You could also specify the body id attribute in the View which wishes to set it:
#{
ViewBag.Title = "Test";
ViewData["BodyID"] = "test";
Layout = "~/Views/Shared/_Layout.cshtml";}
This helps to decouple the view from the controller, the controller (and/or view model) does not need to know about the id attribute of the body tag.
Set the id of the body tag in the master page like so:
<body id="#ViewData["BodyID"]">
Why don't you do in your masterpage:
<body class="<%=ViewData["bodyClass"].toString()%>">
and then set ViewData["bodyClass"] in your Controller actions? That should be equivalent...

Resources