unable to set layout page for a view - asp.net-mvc

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

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);
}

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.

MVC 4: Create controller for Shared view _Layout.cshtml

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.

Why isn't my ViewBag setting available in my view?

I have the following code in my HomeController's Index action, and the code below it in my layout. When I call up the Home/Index view, the UserName property isn't set, but I know it is set in my controller, as I examine the value with a breakpoint.
Controller:
public ActionResult Index()
{
ViewData.Add("UserName", User.Identity.Name);
return View();
}
View:
#{
if (User.Identity.IsAuthenticated)
{
<text>Hello </text>
#ViewBag.UserName <text> - </text>
#Html.ActionLink("Sign Out", "LogOff", "Account", null, new { style = "font-weight: bold;" })
}
}
EDIT: When I try the following view code, as suggested in answers below, I get a the compilation error: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'Display' but appears to have an extension method by that name.
I wonder if this is not some side effect of my nested layouts? The layout for the view cited is _ThreeColumn, and the layout for the latter is _Layout, set as default in _ViewStart.
First off, you should consistantly use viewbag or viewdata in both your controller and view, as it makes your code easier to understand.
public ActionResult Index()
{
ViewBag.Username = User.Identity.Name;
return View();
}
The real problem lies with #Html.Display you dont need the # symbol again. It should be as follows:
#ViewBag.UserName <text> - </text>
change #Html.Display("#ViewBag.UserName") to #ViewBag.UserName
also you can do below for more readability (inside controller)
ViewBag.UserName= User.Identity.Name;
#variableName in razor views automatically produces html encoded text. See below
---A razor view start---
#{
string name= "<b>Praveen</b>";
}
Hello #name
<br><br>
Hello #Html.Raw(name)
---A razor view end---
--output----
Hello <b>Praveen!!</b> ---automatically html encoded
Hello Praveen ---html produced as it is

ASP.NET MVC RenderAction re-renders whole page

How do I prevent renderaction from rendering the masterpage and giving it back to me? I only want it to render 1 section, for eg.
The controller
public ActionResult PaymentOptions()
{
return View(settingService.GetPaymentBanks().ToList());
}
The PaymentOptions View:
#model IEnumerable<Econo.Domain.PaymentBank>
<h2>Payments</h2>
<!-- Stuff here -->
The View
<div class="grid_10">
</div>
<div class="grid_14">
#{Html.RenderAction("PaymentOptions", "Administrator");}
</div>
In grid_14, the header, footer and everything else gets rendered. Is there a way to prevent this?
public ActionResult PaymentOptions()
{
return PartialView(settingService.GetPaymentBanks().ToList());
}
In Razor, partial views and full views have the same extension, so you need to explicitly use the PartialViewResult result type to specify a partial view.
This:
return View(settingService.GetPaymentBanks().ToList());
Has to use the overload so you can specify a master:
return View("PaymentOptions", "", settingService.GetPaymentBanks().ToList());

Resources