Is it possible pass a string from the _ViewStart file to my Layoutfile? - asp.net-mvc-4

I have a Layout File that is used by both my main site and the Admin area. In the Layout I make a call to the MVCSiteMapProvider to generate my layouts. What I would like to do is setup a string in the _ViewStart file that I can pass into the Layout to designate which sitemap to use. Is it possible to pass variables from the _ViewStart down the Layout file and if so how?

Just off the top of my head you could use PageData.
Provides array-like access to page data that is shared between pages, layout pages, and partial pages.
_ViewStart
C#
#{
PageData["MyString"] = "Test!";
}
VB.NET
#Code
PageData("MyString") = "Test!"
End Code
_Layout
C#
#PageData["MyString"]
VB.NET
#PageData("MyString")

You can use ViewBag for it.
In viewstart:
#{
ViewBag.YourString = "any string";
}
In Layout:
#{
var str = (string)ViewBag.YourString;
}

Related

MVC View links stored in config

Is there config setting that I can use as links within sitefinity MVC view? So I have a href's in my view that i dont want to hardcode them in the view cshtml file.
<a href="ConfigSetting">
I think you want to make a custom Config Section
https://www.progress.com/documentation/sitefinity-cms/for-developers-create-a-new-configuration
Then I always do something like this in a static helper class so I can use it in Views and around the site
public static MyConfig MyConfig {
get
{
return Telerik.Sitefinity.Configuration.Config.Get<MyConfig>();
}
}
So then it's
<a href="#Util.MyConfig.ConfigSetting">
Steve
https://www.sitefinitysteve.com

MVC 4.0 using Razor view

How to set the Label Text which is available on _Layout.cstml from different Controller.
For Example
If I want to navigate from Dashboard to ProductList Page. Then My Label Text on _Layout Page should be "Product List". which I am setting from ProductList Controller Index Method.
Thanks in advance.
Use the ViewBag.xyz in _Layout.cshtml. Pass the viewdata from controller action and it will be shown on your html page.
Note:- Please confirm me if you means something else. I will update my answer if this doesn't means what you looking for.
it's look like you want to set the ViewBag.Xyz different different for every controller.
I recommanded you to use ActionFilterAttribute to make it work. The code will something like this.
public class DashboardCustomData : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.ViewBag.xyz = "This is my Dashboard page";
base.OnActionExecuting(filterContext);
}
}
Now put this Actionfilter to every controller. You need to use different different ActionFilter for every controller which have different title. After doing this you never need to set it manually from every controller.
If you don't like to write the multiple filter then try this one.
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string controllerName = filterContext.RouteData.Values["Controller"].ToString();
string ActionName = filterContext.RouteData.Values["Action"].ToString();
if (controllerName == "Dashboard")
{
filterContext.Controller.ViewBag.xyz = "This is my Dashboard page";
}
else
{
}
base.OnActionExecuting(filterContext);
}
You make BaseViewModel class with that prop, set _Layout's model as this type and derive other models from it. Now you can access that prop from any derived model
You use ViewBag or ViewData
You make a #section in Layout view and populate it within Controller's views
If you need Layout's fields in many controllers then usage of #1 looks preferred.
Why does not use this simple approach:
#{
ViewBag.Name = "...";
Layout = "_Layout.cstml";
}
And in your layout set your lable text with #ViewBag.Name
Layout
<label>#ViewBag.Label</label>
Then just assign a value to ViewBag.Label in your controllers, and it will automatically change.
public ActionResult OtherPage()
{
ViewBag.Label = "Other Label";
}
Solution you have suggested works when I am on the same Controllers View but here I want to Change the Text of Controller on Master Page.
In Asp.net We use Master Page Reference on Child Page then we can
easily Change the Text, but how do i do same in MVC... Hope you
understood the scenario. –
If you look at what is generated when you start a new project with the "Internet Template" in MVC 4, you have a way to do it already working for you:
In the element of _Layout, look for <title>#ViewBag.Title - My ASP.NET MVC Application</title>, then in all the different views, you have something like this:
#{
ViewBag.Title = "Contact";
}
<hgroup class="title">
<h1>#ViewBag.Title.</h1>
<h2>#ViewBag.Message</h2>
</hgroup>
All you need to do is to move/duplicate ViewBag.Title out of the HEAD intot the BODY, or create your own ViewBag variable, and set it's values from your different views. Obviously you could just as well set the value from the controller, but I don't see the need, as this really has to do exclusively with the user interface, which is the Views job.
Finally, if I may, don't try to duplicate in MVC what you're used to with ASP.NET. The paradigms are way too differents, chances are that you are going to waste time while there might be an easy MVC way to achieve the same thing.
It seems to me this is just an example of it.....

ASP.NET MVC 4 - _Layout Master pages loading in views not configured to do so

In my ASP.NET MVC 4 application, I have created a view (not a partial) that shows only a grid of data for printing. This acts as a print-safe view. This is enabled by a simple tag that sends you to the view. The problem is, the master page layouts are being loaded in the view.
Is there anywhere in an ASP.NET MVC 4 app, that could be secretly configuring my views to include the master _layout?
Yes. check your ~/Views/_ViewStart.cshtml file, you may find something like this:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
You can override it in your View though:
#{
Layout = null;
}
Or if your View is rendered through an Action, try to return PartialView() instead of View():
public ActionResult RenderGrid()
{
...
return PartialView(); // return View without invoking _ViewStart.cshtml
}
If you have a non-partial view, and unless you override the layout, whatever is configured in _ViewStart.cshtml will be used. You can override this by adding this to your view:
#{
Layout = null;
}
Since my _Layout was entirely in 1 element, i was able to use javascript to hide it. This not the answer I like, because its not a solution, it is a cheap work around.

The layout page could not be found

I get the error: The layout page "_Layout" could not be found at the following path: "~/Views/Home/_Layout".
But layout page is at this path: "~/Views/Shared/_Layout"
What can it be for problem?
I just started the project and it look like this:
Controller:
namespace Testing.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
}
Index view:
#model dynamic
#{
ViewBag.Title = "title";
Layout = "_Layout";
}
<h2>title</h2>
_ViewStart.cshtml:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Solution Explorer:
Make sure that in your ~/Views/_ViewStart.cshtml file you have set the correct path:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Also if in your views you are overriding the layout ensure that the proper path is specifid for this layout. And in addition to that there could be some server side code which is setting the layout (such as custom action filters, or the ViewResult overload which allows to specify a layout, ...).
UPDATE:
You seem to have set the Layout like this:
#{
ViewBag.Title = "title";
Layout = "_Layout";
}
You need to specify the location to the layout as absolute path:
#{
ViewBag.Title = "title";
Layout = "~/Views/Shared/_Layout.cshtml";
}
But an even better way is to get rid of this Layout setting in your Index view:
#{
ViewBag.Title = "title";
}
Now the value from your _ViewStart.cshtml will be used.
I had this same problem appear once i had deployed my service, but could not replicate locally. For me the issue was that the View had None set for its build action. So the error was correct in that the file did not exist on the server, despite it existing in the repository.
Solution to fix this:
Right click on missing view and open Properties View Properties
Ensure Build Action is set to Content
Update csproj file to match new Build Action. .csproj file
Using the wrong tilde can also produce this error.
Make sure you use the tilde "~" on your keyboard.
If you copied the code from a book, the tilde could be an invalid character (note that there are more than one tilde in Unicode).
In your case i cant see the _layout.cshtml .. you have to give the correct path to the _layout.cshtml file.. if the file is exist in the same views folder with the _ViewStart.cshtml no need to add .cshtml part.
but if you reference it from another folder make sure to add .cshtml part like this _layout.cshtml ..
so you have to add correct path whare the _layout.cshtml located. like below
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Instead of this
#{
ViewBag.Title = "title";
Layout = "_Layout";
}
the index view will be like that
#{
ViewBag.Title = "title";
Layout = "~/Views/Shared/_Layout.cshtml";
}
Maybe you have a PartialView and now you want to return View instead of PartialView in your ActionResult Method.

How do you define a layout for specific areas in ASP.Net MVC 4?

I'm playing a little catch up here as I went straight from MVC2 to MVC4, so learning Razor and everything else all at once.
I'm using an admin area in this new application, and I noticed when I went to the controller in the admin area it rendered without any layout. I tried copying the _Layout.cshtml into the shared view folder of the area, but it still renders with no layout. I tried searching, but can't find any information as to how you set a layout to be used for an area.
I know I can do this on a specific view, but I want to set it once for the entire area
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
You have to have file _ViewStart.cshtml under folder Views in your area.
This file would have something like this in it:
#{
Layout = Request.IsAjaxRequest() ? null : "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}