Layout page not loading in asp.net mvc - 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);
}

Related

MVC5 How to do a post from partialview?

My partialview:
#model Alina_2017.Models.DropDownModel
<h2>Groepen</h2>
<div>
<div>
#using (Html.BeginForm("SelectGroup", "~/Controllers/WerkvormController"))
{
#Html.DropDownListFor(x => x.selectedItem, new SelectList(ViewBag.groepen, "id", "Naam"), "Select", new { #class = "form-control" })
<input type="submit" id="zoekgroep" value="Zoeken" />
}
</div>
</div>
My main view:
#model Alina_2017.Models.WerkvormModel
#{
ViewBag.Title = "Index";
}
#Html.Partial("~/Views/DropDown/Groepen.cshtml")
//More irrelevant html
My controller:
public ActionResult Index()
{
ViewBag.groep1 = convertWerkvorm(db.Werkvormens.Where(f => f.GroepenWerkvormID == 1).ToList());
ViewBag.groep2 = convertWerkvorm(db.Werkvormens.Where(f => f.GroepenWerkvormID == 2).ToList());
ViewBag.groep3 = convertWerkvorm(db.Werkvormens.Where(f => f.GroepenWerkvormID == 3).ToList());
setViewBags();
return View();
}
[HttpPost]
public ActionResult SelectGroup(DropDownModel model)
{
// the value is received in the controller.
var selectedItem = model.selectedItem;
Debug.WriteLine(selectedItem);
return View("Index");
}
I'm getting a HTTP Error 404.0 - Not Found. Is it possible to call an action from a different controller? The reason it's in a partial view is because I'm using two different models + I'll be using the partialview in multiple other views (at least once I get it to work).
Your controller's name is wrong.
Replace
#using (Html.BeginForm("SelectGroup", "~/Controllers/WerkvormController"))
with
#using (Html.BeginForm("SelectGroup", "Werkvorm"))
You can verify the actual post URL if you view your source in browser, or check network tab in the browser's development tools.
The second argument to the BeginForm() method is simply the name of the controller, not its file:
#using (Html.BeginForm("SelectGroup", "Werkvorm"))
{
}
You can post to any server-side action from anywhere. There's no limit based on how the view is rendered because once everything is rendered it's all just client-side markup no matter where it came from.
As a learning exercise, examine the actual rendered markup in your browser's debugging tools and see the URLs created for the forms. Regardless of how the partial views are arranged, which controller returned the view, what the models are, etc... It's all just HTML in the end. You can even manually write a simple .html file with a form on it which successfully posts to a server-side ASP.NET MVC action.

Set Layout for Single paged application in ASP.NET MVC

I'm new to MVC and trying to create a single paged application.
My Content in _Layout.cshtml
<section class="content-wrapper main-content clear-fix">
#Html.Action("Index", "MenuNavigation", "U01")
#RenderBody()
</section>
Note: #Html.Action("Index", "MenuNavigation", "U01") is the place where I'm rendering my dynamic Navigation menu from database(will load only once from DB)
Below is my _MainLayout.cshtml that uses _Layout.cshtml as master page
#{
ViewBag.Title = "";
Layout = "_Layout.cshtml";
}
<div id="MainPageContent">
#RenderBody()
</div>
Here is my Home\Index view will looks like
#{
ViewBag.Title = "Home Page";
if (ViewBag.IsRequestFromLoginPage !=null && ViewBag.IsRequestFromLoginPage)
{
Layout = "~/Views/Shared/_MainLayout.cshtml";
}
else
{
Layout = null;
}
}
#Html.AntiForgeryToken()
<h2>Home</h2>
here is the respective Home controller
public ActionResult Index()
{
ViewBag.IsRequestFromLoginPage = TempData[Constants.TempData.IsUserFromLogin];
TempData[Constants.TempData.IsUserFromLogin] = false;
return View();
}
When user logged-in to my site I wanted to render my Home\Index and load my Nav Menu(partial content) only on the first load.
If User select any of the menu item, I'm doing AJAX call to update the MainPageContent in _MainLayout.cshtml by this way I'm trying to create the single paged application but problem is when User refresh the page from browser URL. For example: If User is in "MyApp.com/Home" and refresh the browser it causes my application looses its _layout (render only home content) because of the ViewBag.IsRequestFromLoginPage check in my Home page.
Not sure is this the correct way to achieve this. Is there any alternative to do so. Any help will be appreciated. Thanks!
Here is how my Layout is looks like>
#Daniel
Your code is a little bit redundant, why do you have #RenderBody() in _MainLayout.cshtml if you say its master layout is _Layout.cshtml which also calls #RenderBody? instead of using #Html.Action and #RenderBody() use Html.RenderAction (When calling a differente method that's not index) and don't specify a Layout for your that view, in that same way, get rid of all the validations to see if it comes from the login page or not, note that Index method always enters by default so there's actually not even a need of calling any type of HTML action if you're accessing your Home Controller, you could leave your main layout like this
<section class="content-wrapper main-content clear-fix">
<div id="MainPageContent">
#RenderBody()
</div>
</section>
and your Index view like this:
#Html.AntiForgeryToken()
<h2>Home</h2>
In a different ActionResult method you can load the same view as Index like this:
public ActionResult update(){
return View("Index");
}
You'd then make the AJAX call to fill the "MainContentPage" div with that method instead of "Index".

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

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.

asp.net MVC partial view controller action

I'm very new to web app development and I thought I would start with recent technology and so I'm trying to learn asp.net as-well as the MVC framework at once. This is probably a very simple question for you, MVC professionals.
My question is should a partial view have an associated action, and if so, does this action get invoked whenever a normal page uses RenderPartial() on the partial view?
While you can have an action that returns a partial view, you don't need an action to render a partial view. RenderPartial takes the partial view and renders it, using the given model and view data if supplied, into the current (parent) view.
You might want an action that returns a partial view if you are using AJAX to load/reload part of a page. In that case, returning the full view is not desired since you only want to reload part of the page. In this case you can have the action just return the partial view that corresponds to that section of the page.
Standard mechanism
Making use of partial view within a normal view (no action needed)
...some html...
<% Html.RenderPartial( "Partial", Model.PartialModel ); %>
...more html..
Ajax mechanism
Reloading part of a page via AJAX (note partial is rendered inline in initial page load)
...some html...
<div id="partial">
<% Html.RenderPartial( "Partial", Model.PartialModel ); %>
</div>
...more html...
<script type="text/javascript">
$(function() {
$('#someButton').click( function() {
$.ajax({
url: '/controller/action',
data: ...some data for action...,
dataType: 'html',
success: function(data) {
$('#partial').html(data);
},
...
});
});
});
</script>
Controller for AJAX
public ActionResult Action(...)
{
var model = ...
...
if (Request.IsAjaxRequest())
{
return PartialView( "Partial", model.PartialModel );
}
else
{
return View( model );
}
}
The accepted answer is completely correct, but I want to add that you can load your partial view using jQuery load. Less configuration needed, if you don't want to consider concurrency.
$("#Your-Container").load("/controller/action/id");
I was able to achieve something similar with this logic.
Within the .cshtml
#Html.Action("ActionMethodName", "ControllerName");
Within the controller
[Route("some-action")]
public ActionResult ActionMethodName()
{
var someModel = new SomeModel();
...
return PartialView("SomeView.cshtml", someModel);
}
And that's it.
If you need to pass values from the .cshtml to the action method then that is possible to.
The answer is no. But sometimes you need some controller action behind a partial view. Then you can create an actionMethod wich returns a partial view. This actionMethod can be called within another view:
#Html.Action("StockWarningsPartial", "Stores")
The actionmethod can look like:
public ActionResult StockWarningsPartial()
{
....
return View("StockWarningsPartial", warnings);
}
and the view 'StockWarningsPartial.cshtml' starts with:
#{
Layout = null;
}
to make it not render your surrounding layout again.
public ActionResult GetStateList(int country_id)
{
List<stateDTO> stateList = new List<stateDTO>();
stateList = bll.GetState(country_id);
ViewBag.sList = new SelectList(stateList, "state_id", "State_Name");
return PartialView("DisplayStates");
}

Resources