I have a regular application _Layout.cshtml and #RenderBody() to render all views inside, but only for the Login view need to be render as full page not inside #RenderBody().
Regards
You need to set the Layout property to null at the beginning of your view.
So, your Login.cshtml file should start with:
#model YourModel
#{
Layout = null;
}
You may not want to have no layout for your view. You probably want a custom layout (something like _LayoutFullPage.cshtml
<!DOCTYPE html>
<head>
<title>SOMETHING HERE ALONG WITH OTHER HEAD ATTRIBUTES</title>
</head>
<body>
#RenderBody()
</body>
</html>
and then use that layout in the view with
#{
Layout = "~/Views/Shared/_LayoutFullPage.cshtml";
}
Other options are to set the view as null in the cshtml (as suggested by RePierre) or to call return PartialView(model); within your controller.
Related
I'm learning ASP.NET MVC. I have a service folder where I have a class which reads data from an XML file. I've created a controller that I think (?) should work, and I'm attempting to create a view for this as well but for some reason I can't get intellisense to autocomplete the #model which makes me think I've done something wrong. Additionally, when I try access model properties from the view (eg Model.Description - if that is even the syntax?) I get numerous missing { and } errors. What am I doing wrong?
Controller:
public ActionResult Index()
{
NewsReader newsReader = new NewsReader(); //Read news from file
var newsItems = newsReader.GetNewsItems();
return View(newsItems);
}
And so far this is all I have for the view:
#Model IEnumerable<TestSite.Services.News.NewsItem>
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body>
<div>
<ul>
#foreach(Model.Description)
{
}
</ul>
</div>
</body>
</html>
Refer to #Erik Funkenbusch explanation of MVC #model meaning
The # sign is a directive to tell the Razor engine that what follows
is code, and it should compile that rather than simply write it to the
output.
so when you type
#model blah This is compiled by razor, and tells the Razor engine that
the type of the model is 'blah', so that when you use the keyword
Model (note the capital M and you would have to use the # sign as
well) it will refer to the model you have defined (in this case blah).
Therefore corrections should be taken as following:
//#Model IEnumerable<TestSite.Services.News.NewsItem>
#model IEnumerable<TestSite.Services.News.NewsItem>
//#foreach(Model.Description)
#foreach(var item in model.Description)
{
}
I recommend you to read Getting Started with ASP.NET MVC 5 to learn some basics about ASP.NET MVC 5. Razor engine is used by MVC 5 for the view styling.
And have some idea here
Essentially, my issue is this: I'm trying to display a ViewData variable on an ASPX page, but I'm getting "ViewData does not exist in the current context."
What led me here? I'm new to MVC, and am following an online tutorial. In Lesson 1, he has us create a view by going to Add > View, which creates a Razor view. Then, in the next tutorial, he's teaching us to use ViewData to pass a variable to the view. But, when he displays it, he's doing so using the <%: %> method, which doesn't work on my Razor page. So, I try to create an ASPX page in my View folder by creating a Web Form the traditional way. When I do that, I get the error mentioned above. I have successfully figured out how to get the Razor method working in my cshtml page, but what is the appropriate method should I want to do it the "angle bracket - percent" way?
Edit: Basically, all I did was follow the instructions for Tutorial 2 in the following walk-through. He doesn't really explain HOW he created the ASPX page (or that he used an ASPX page at all). If anyone knows of a better tutorial that's more complete, I'm all ears.
http://www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in#Lab1:- Creating a simple hello world ASP.NET MVC Application
Edit2: By request, here's my code:
ASPX:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="MVCLearn.Views.FirstMVC.Index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%= ViewData["vwTest"] %>
</div>
</form>
</body>
</html>
And here is my Controller:
public class FirstMVCController : Controller
{
// GET: FirstMVC
public ActionResult Index()
{
ViewData["vwtest"] = "Something";
return View();
}
public ActionResult SayHello()
{
return View("HelloView");
}
}
<%= ViewData["vwTest"] %>
ViewData["vwtest"] = "Something";
vwTest != vwtest
You should write same name
<%= ViewData["vwTest"] %>
ViewData["vwTest"] = "Something";
In my MVC3 solution, I am attempting to implement the solution in this answer to dynamically add a CSS reference to the _Layout.cshtml file from within a partial view.
Here is a snippet of my _Layout file:
<!DOCTYPE html>
<html>
<head>
#Html.RenderStyles()
</head>
<body>
<div id="header-wrapper">
#Html.Action("Header", "Shared")
</div>
<div class="container">
#RenderBody()
</div>
#RenderSection("scripts", required: false)
</body>
</html>
and in the partial view (that is rendered by the #Html.Action("Header", "Shared") bit):
#{
Html.AddStyle(Model.Company.CSSPath);
}
While the HtmlExtension methods are working as expected, it seems that the events are occuring in the incorrect order.
The #Html.RenderStyles() in the _Layout head is occuring before the Partial View has time to add the CSS file to the Styles. I was under the impression that Partial Views were rendered before the _Layout.
I'm guessing that the cause has to do with the fact that I'm rendering my Partial View with an Html.Action call. If I don't do so, however, then how can I populate the Partial view with its desired model?
Per Matt Razza's comment, if I were to render the partial view the "normal way" using:
#Html.Partial("ViewName", model)
I'm then required to give my _Layout a Model and then force all other models to inherit from that Model? Is that truly the best option?
I have an MVC Masterpage.
Within the body, I have a placeholder for a title that all the views using this masterpage populate:
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
But as well as this title being used how it is, I want the exact same text to go in the title section of the head tag.
Of course, I could use another placeholder and make every single view specify the same content twice, but it would be better if some code could magically copy the literal text out of the placeholder and put it into the head title tag as well.
Obviously Javascriptis no use, because google wont process it to the page title. So I need to do this serverside.
In general, you would bind these values in the view to data either on a model or perhaps in a ViewBag (or ViewData for older versions of MVC) or something of that nature. So in your Layout (Master Page? Must be an older version of MVC?) you might have a couple of references to a ViewBag (or ViewData) value. Something like this:
<html>
<head>
<title><%= ViewBag.PageTitle %></title>
</head>
<body>
<div id="somePageTitle"><%= ViewBag.PageTitle %></div>
</body>
</html>
or:
<html>
<head>
<title><%= ViewData["PageTitle"] %></title>
</head>
<body>
<div id="somePageTitle"><%= ViewData["PageTitle"] %></div>
</body>
</html>
Then in your controller you would set that value as needed:
ViewBag.PageTitle = "This is a page title";
or:
ViewData["PageTitle"] = "This is a page title";
That would bind that value to both locations in the view. It's more common to use ViewBag (or other constructs, like ViewData and TempData, often depending on the MVC version being used) for elements in the layout (Master Page) and to use view models for elements in the specific view.
First off David's answer is right
Actual implementation would be whatever.master
<!DOCTYPE html>
<html>
<head>
<title><%= ViewData["Title"] %></title>
</head>
<body>
<asp:ContentPlaceHolder ID="TitleContent" runat="server"/>
</body>
</html>
In your View
<asp:Content ContentPlaceHolderID="TitleContent" runat="server">
<%= ViewData["Title"] %>
</asp:Content>
Change it in the controller
public ActionResult Index()
{
ViewData["Title"] = "My new title";
return View();
}
Or set it in the View itself
<script runat="server">
ViewData["Title"] = "My new title";
</script>
<asp:Content ContentPlaceHolderID="TitleContent" runat="server">
<%= ViewData["Title"] %>
</asp:Content>
Since these are variable, use them anywhere in your page you like
// These are the same
ViewData["Title"] = "My new title";
ViewBag.Title = "My new title";
// These are the same
<%= ViewData["Title"] %>
<%= ViewBag.Title %>
public Dictionary<string, object> ViewData
public dynamic ViewBag // .NET 4.0 +
Thanks for all your help.
I wanted to set the data in the view because I have different controllers using the same view, and also some controllers returning different views depending on the logic, so it made sense to define the page title on the view where it is a known entity, especially since it is usually not model data and is thus always a constant text for that page.
One complication, is that if the view was to be set as a dynamic piece of data taken from the model, that hadn't been calculated yet (such as a deferred execution iqueryable), then the masterpage never realizes the value unless you populate it in the view at a higher point in the page.
In other words, the view has to set the viewdata in script in a content placeholder ABOVE where it will be in the masterpage. You cannot set the viewdata[] property in the body content if the masterpage uses it in the head section.
This works:
<head>
<asp:Placeholder ID="HeadContent" runat="server"/>
<title><%: ViewData["PageTitle"] %></title>
</head>
<body>
<%: ViewData["PageTitle"] %>
</body>
Then in the view you can set the title:
<asp:Content ContentPlaceHolderID="HeadContent" runat="server">
<% ViewData["PageTitle"] = Model.SomeValueThatHasDefferedExecution; %>
</asp:Content>
I'm refactoring all my partial views to use the leading underscore naming convention (didn't know about this convention when I got started, don't want to deviate from such a plain convention). But I've come across the generated (razor, C#) "Error.cshtml" view in \Views\Shared and I noticed it looks like it may be a partial view (no html / body tags) yet doesn't follow the convention:
#model System.Web.Mvc.HandleErrorInfo
#{
ViewBag.Title = "Error";
}
<h2>
Sorry, an error occurred while processing your request.
</h2>
I also notice it doesn't specify a Layout, yet sets the ViewBag.Title property as if it were going to be used by a Layout. What's the deal?
Strange. My generated Error.cshtml looks like a "normal" view with HTML body:
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
</head>
<body>
<h2>
Sorry, an error occurred while processing your request.
</h2>
</body>
</html>
I think in your case the layout is not set to null, hence the default layout will be used that contains the HTML wrapping elements.
But it is still a "normal" view (not partial).